[Unity] Instantiate and Destroy usage

Instantiate usage

Instantiate(obj, new Vector3(0, 10, 0), Quaternion.identity)

If you want to perform further operations on the generated objects, refer to the following

 public Rigidbody obj;
 void Update () {
    
    
	if(Input .GetKeyDown(KeyCode.Space ))
    {
    
    
     Rigidbody rig=Instantiate(obj, new Vector3(0, 10, 0), Quaternion.identity) as Rigidbody;
     rig.AddForce(new Vector3(0, 0, 6), ForceMode.Impulse);
    }
}

Destroy function

Destroy(gameObject);//销毁gameObject

The gameObject is the object to be destroyed, and the latter refers to how many seconds after the object is generated, it will be destroyed, and 2f means that it will be destroyed after two seconds

Destroy(gameObject,2f);

Guess you like

Origin blog.csdn.net/qq_43666766/article/details/104952710