I am trying to create a public float timer that senses when the particular prefab has not been there for a certain amount of time. Then, it will replace that particular object.
The goal is to randomly spawn 10 of these cubes across the game board. Then when the player picks one up, it disappears. Then after say, 2 seconds, a new cube should be placed on the board.
Right now the Player already moves around the board just fine, and can pick up the cubes. When the player picks up a Cube, a new cube is automatically placed on the board with no delay.
This script will be attached to the cube prefab directly.
Apologies for the appearance of the code in advance.
using UnityEngine;
using System.Collections;
public class RandomPlacement : MonoBehaviour
{
public GameObject prefab;
private GameObject prefabClone;
public int count;
private GameObject[] getCount;
public float timer = 2;
// Instantiate the Prefab somewhere between - 10, 10 on the x, and z planes, with a height of 0.5 in y.
void Update ()
{
getCount = GameObject.FindGameObjectsWithTag("PickUp");
count = getCount.Length;
if (count < 10)
{
timer -= Time.deltaTime;
if (timer <= 0f);
{
Vector3 position = new Vector3(Random.Range(-9.0f, 9.0f), 1.0f, Random.Range(-10.0f, 9.0f));
prefabClone = Instantiate(prefab, position, Quaternion.identity) as GameObject;
timer = 2;
}
}
}
}
The code for the collision between the pick-up and ball is given by:
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);
}
}
}
The "Cube" prefabs are all given the tag "PickUp" This script is attached to the "Player"
Timer
variable to betimer
to conform to standard naming convention. \$\endgroup\$