# How to Implement a Timer for my Prefab for Roll-a-Ball Game (C# in Unity)

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"

• I would rename your Timer variable to be timer to conform to standard naming convention. – Draco18s no longer trusts SE Dec 11 '15 at 20:47
• Can you put the code of finger touch where object disappears after touch? – Hamza Hasan Dec 11 '15 at 20:52
• Will add it now – Luke Taylor Dec 11 '15 at 21:00
• have updated "Timer" to 'timer' to reflect standard naming convention – Luke Taylor Dec 11 '15 at 21:09
• Clarification question: you want a new cube to be spawned (after the delay) just when one is picked. Let's say, if cube 2 is pciked, a new cube 2 will be placed after X seconds. If in the meanwhile cube 7 is picked, a new cube 7 will be placed X seconds after that, and so on. – MAnd Dec 11 '15 at 21:41

I don't know what is your optimization logic is here, but below is the limited optimized and required code for you according to code you did provide.

void Update()
{

}

void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag("PickUp"))
{
other.gameObject.SetActive(false);// other.gameObject should destroy here by the way.
getCount = GameObject.FindGameObjectsWithTag("PickUp");
count = getCount.Length;
if (count < 10)
{
Invoke("SpawnObject",2.0f);
}
}
}

void SpawnObject()
{
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;
prefabClone.tag = "PickUp";
}

• So you would put the re spawn inside the movement script. I suppose that should work :) I'm sure it would probably speed things up to. Thank you very much. I am new to Unity so haven't been thinking too much about optimization yet. – Luke Taylor Dec 11 '15 at 21:23
• If that is in another script, use some delegates fun. Well, give it a try. Let me know if you get in any trouble. And if you find this useful then mark it as answer :) – Hamza Hasan Dec 11 '15 at 21:25
• Yes, you should make it 10 in start. Execute SpawnObject() in for-loop of 0-9 in Start() – Hamza Hasan Dec 11 '15 at 21:53
• Thanks that works. Then should I use a start() to Initialize 10 of these at random initially? – Luke Taylor Dec 11 '15 at 21:54
• done. One final question. Now it works and I included a for loop to spawn 10 cubes, it seems that I cannot pick up the cloned prefabs. – Luke Taylor Dec 11 '15 at 22:03