How to destroy instatiate objects?
hi.. im not a programmer and the code that i get is made from pieces of code that i get from the internet, so i want the "newbullet" to destry it self after 2 seconds or when it hit something but im not able to do that because i can't assign it as GameObject, can someone help? also im not able to decide the "timebetweenshots" if someone know how to i'll appreciate.
Answer by RUNDEN · Jun 03, 2018 at 08:42 PM
After line 33, you can add "Destroy(newbullet, 5f);" - This will make the bullet script stop existing after 5 seconds. (You can change the 5 to any number, it doesn't need to be 5) However, I think that has a GameObject, so if the first does not work, you can try "Destroy(newbullet.gameObject, 5f);", which removes the whole gameobject that the bullet is on.
For making the bullet get destroyed if it hits something, you can add to the bullet script the lines:
void OnCollisionEnter(Collision collision) {
Destroy(this);
}
Or, if like above, it is a gameobject you want to destroy, change Destroy(this); to Destroy(this.gameObject);
For finding the time between shots, you want to put timebetweenshots = timebetweenshots + time.deltaTime; in the root of the update root. And you want to put timebetweenshots = 0f; after line 32.
Hope this helps!
Your answer
Follow this Question
Related Questions
Destroy Instantiate is not working 0 Answers
Canon Ball shooting with Instiate 1 Answer
instantiate gameobject after destroyed 0 Answers
How to destroy on exiting PlayMode/EditMode? 1 Answer
How to destroy only a clone in Unity 3d? 0 Answers