0
\$\begingroup\$

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"

\$\endgroup\$
  • \$\begingroup\$ I would rename your Timer variable to be timer to conform to standard naming convention. \$\endgroup\$ – Draco18s Dec 11 '15 at 20:47
  • \$\begingroup\$ Can you put the code of finger touch where object disappears after touch? \$\endgroup\$ – Hamza Hasan Dec 11 '15 at 20:52
  • \$\begingroup\$ Will add it now \$\endgroup\$ – Luke Taylor Dec 11 '15 at 21:00
  • \$\begingroup\$ have updated "Timer" to 'timer' to reflect standard naming convention \$\endgroup\$ – Luke Taylor Dec 11 '15 at 21:09
  • \$\begingroup\$ 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. \$\endgroup\$ – MAnd Dec 11 '15 at 21:41
1
\$\begingroup\$

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";
}
\$\endgroup\$
  • \$\begingroup\$ 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. \$\endgroup\$ – Luke Taylor Dec 11 '15 at 21:23
  • \$\begingroup\$ 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 :) \$\endgroup\$ – Hamza Hasan Dec 11 '15 at 21:25
  • \$\begingroup\$ Yes, you should make it 10 in start. Execute SpawnObject() in for-loop of 0-9 in Start() \$\endgroup\$ – Hamza Hasan Dec 11 '15 at 21:53
  • \$\begingroup\$ Thanks that works. Then should I use a start() to Initialize 10 of these at random initially? \$\endgroup\$ – Luke Taylor Dec 11 '15 at 21:54
  • \$\begingroup\$ 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. \$\endgroup\$ – Luke Taylor Dec 11 '15 at 22:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.