Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- public class SpawnFood : MonoBehaviour {
- public GameObject foodPrefab;
- public List<GameObject> foodList;
- public int MaxFoodSpawn = 10;
- public int CurrentAmountOfFood = 0;
- public float timeLeft = 5f;
- public Vector3 center;
- public Vector3 size;
- private void Start()
- {
- foodList = new List<GameObject>();
- }
- private void Update()
- {
- timeLeft -= Time.deltaTime;
- if ( timeLeft < 0)
- {
- for (int i = 0; i < MaxFoodSpawn; i++)
- {
- if (CurrentAmountOfFood < 10)
- {
- CheckForList();
- CreateGizmos();
- CurrentAmountOfFood++;
- }
- }
- timeLeft = 5f;
- }
- }
- public void CheckForList()
- {
- for (int i = foodList.Count - 1; i > -1; i--)
- {
- if (foodList[i] == null)
- {
- Debug.Log(i);
- foodList.RemoveAt(i);
- }
- }
- }
- public void CreateGizmos()
- {
- Vector3 pos = center + new Vector3
- (Random.Range(-size.x / 2, size.x / 2),
- (Random.Range(-size.y / 2, size.y / 2)),
- (Random.Range(-size.z / 2, size.z / 2)));
- //Spawn food
- foodList.Add((GameObject)Instantiate(foodPrefab, pos, Quaternion.identity));
- }
- private void OnDrawGizmosSelected()
- {
- Gizmos.color = new Color(1, 0, 0, 0.5f);
- Gizmos.DrawCube(center, size);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement