Advertisement
Guest User

Untitled

a guest
Jul 22nd, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.64 KB | None | 0 0
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4.  
  5. public class SpawnFood : MonoBehaviour {
  6.  
  7.     public GameObject foodPrefab;
  8.     public List<GameObject> foodList;
  9.  
  10.     public int MaxFoodSpawn = 10;
  11.     public int CurrentAmountOfFood = 0;
  12.  
  13.     public float timeLeft = 5f;
  14.  
  15.     public Vector3 center;
  16.     public Vector3 size;
  17.  
  18.     private void Start()
  19.     {
  20.         foodList = new List<GameObject>();
  21.     }
  22.  
  23.     private void Update()
  24.     {
  25.         timeLeft -= Time.deltaTime;
  26.         if ( timeLeft < 0)
  27.         {
  28.             for (int i = 0; i < MaxFoodSpawn; i++)
  29.             {
  30.                 if (CurrentAmountOfFood < 10)
  31.                 {
  32.                     CheckForList();
  33.                     CreateGizmos();
  34.                     CurrentAmountOfFood++;
  35.                 }
  36.             }
  37.             timeLeft = 5f;
  38.         }
  39.     }
  40.  
  41.     public void CheckForList()
  42.     {
  43.         for (int i = foodList.Count - 1; i > -1; i--)
  44.         {
  45.             if (foodList[i] == null)
  46.             {
  47.                 Debug.Log(i);
  48.                 foodList.RemoveAt(i);
  49.             }
  50.         }
  51.     }
  52.  
  53.     public void CreateGizmos()
  54.     {
  55.         Vector3 pos = center + new Vector3
  56.             (Random.Range(-size.x / 2, size.x / 2),
  57.             (Random.Range(-size.y / 2, size.y / 2)),
  58.             (Random.Range(-size.z / 2, size.z / 2)));
  59.  
  60.         //Spawn food
  61.         foodList.Add((GameObject)Instantiate(foodPrefab, pos, Quaternion.identity));
  62.     }
  63.  
  64.     private void OnDrawGizmosSelected()
  65.     {
  66.         Gizmos.color = new Color(1, 0, 0, 0.5f);
  67.         Gizmos.DrawCube(center, size);
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement