generating object stop when update function occur???

i want to generate object at random time at random location
but when update function occur(click the object to score), it stop generate object?how can i modify it?
thx for any help.

ps:i have found that the new object cloned is from the previous object, if the previous object is distroyed,
instantiate would stop.So my question is how to continous instantiate if the previous object is destroyed?

var prefab : GameObject;
  
  
    function Start () {
    
    yield WaitForSeconds (Random.Range(0.0, 2.0)); 
        var position: Vector3 = Vector3(Random.Range(-10.0, 10.0), Random.Range(-10.0, 10.0),0);
        Instantiate(prefab, position, Quaternion.identity);
        Destroy (gameObject, 0.5);
     
    }
    
    function Update (){
    
      
     //this if check for the mouse left click
   if (Input.GetMouseButtonDown(0)){
    var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    var hit: RaycastHit;
    if (Physics.Raycast(ray, hit)){
      Destroy(hit.transform.gameObject);
      Debug.Log("score+1");
      
    }
  }
  }

Did this code generate more then one object ever? Start method calls only once, when object is created.

try this sample:

 var prefab : GameObject;
     
        function RandomCheck () {
                /// any random logic here, this method should return true periodically
                /// it's a little bit crazy, but should works.
                return Random.Range(0, 100) == 0;
        }
       
        function Update (){
       
        if(RandomCheck())
        {
            var position: Vector3 = Vector3(Random.Range(-10.0, 10.0), Random.Range(-10.0, 10.0),0);
            Instantiate(prefab, position, Quaternion.identity);
            Destroy (gameObject, 0.5);
        }
         
         //this if check for the mouse left click
       if (Input.GetMouseButtonDown(0)){
        var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        var hit: RaycastHit;
        if (Physics.Raycast(ray, hit)){
          Destroy(hit.transform.gameObject);
          Debug.Log("score+1");
         
        }
      }
      }

Firstly you need to write this

yield WaitForSeconds (Random.Range(0.0, 2.0)); 

        var position: Vector3 = Vector3(Random.Range(-10.0, 10.0), Random.Range(-10.0, 10.0),0);

        Instantiate(prefab, position, Quaternion.identity);

        Destroy (gameObject, 0.5);

code in update function.

Secondly delete this line of code Destroy (gameObject, 0.5); from here. If you want to destroy the gameobject after some time write different script attch that with Prefab of gameobject.

You can’t yield in Update.

Oh sorry for that mistake.

you can write code this type.

function Update()
{
  Test();
}

function Test()
{
  yield WaitForSeconds (Random.Range(0.0, 2.0)); 
  
  NewObj();
}

function NewObj()
{
   var position: Vector3 = Vector3(Random.Range(-10.0, 10.0), Random.Range(-10.0, 10.0),0);
  
   Instantiate(prefab, position, Quaternion.identity);

}

That’s still a yield within Update. Wrapping it in another method call does nothing. :slight_smile: If you need to continually execute something on a timed basis you can either use Start as a Coroutine, start a coroutine in Start, or use InvokeRepeating.

Ok Thanks @KelsoMRK