Making a prefab fall randomly and then destroy
So this is the code i have now What i want to achieve basically is a prefab falling from a certain height and randomly in the x direction and to be destroyed when it hits the ground, is there a better way to generate the prefabs because if i put the Invoke in update it generates waay too many ??
Also any help on the destroying part would be appreciated.
Thanks
using UnityEngine; using System.Collections;
public class NewBehaviourScript : MonoBehaviour {
 public GameObject prefab;
 float xPosition;
 void Createnow () {
     xPosition=Random.Range(-10,10);
     Instantiate(prefab, new Vector3(xPosition, 5,0), Quaternion.identity);
 }
 // Update is called once per frame
 void Update () {
     Invoke("Createnow",Random.Range(10f,15f));
 }
}
               Comment
              
 
               
              Answer by Ali-hatem · Apr 27, 2016 at 09:28 AM
 float repeatRate;
 void Start(){
  InvokeRepeating("Createnow", 1,repeatRate);
 }
 void Createnow () {
      repeatRate=Random.Range(-10f,15f);
      xPosition=Random.Range(-10f,10f);
      Instantiate(prefab, new Vector3(xPosition, 5,0), Quaternion.identity);
  }
& for destroying you can destroy after time Destroy(gameObject,5f);or by collision : 
     void OnCollisionEnter(Collision other){
     // you can immediately destroy the object once collided with any thing by :
     Destroy(gameObject);
         // but if you want to destroy only if collided with specific object use : 
         if(other.gameObject.tag == "Ground"){
             Destroy(gameObject);
         }
     } 
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                