Spawn an object and then destroy it
So, i am a beginner at scripting, and i asked this question before but i didn't found the answer. Instead, the answers made me even more confuse!
Basically, i want to make a object spawn when the player clicks with the mouse (mousefire1), and then, after a specific time that i can change, the object is destroyed. Can someone at least make the script for me? Or give me a tutorial?
Because i hate when someone say "You need to do this" and then start to say things that doesn't make sense, and then everyone start to to fight and after 3 hours my question is abandoned forever...
For starters you need to instantiate an object after getting the Input from the mouse click, so read these:
https://docs.unity3d.com/ScriptReference/Object.Instantiate.html https://docs.unity3d.com/ScriptReference/Input.GetButtonDown.html
$$anonymous$$eep in $$anonymous$$d you will need to supply a position and rotation when instantiating, position I am taking a guess is maybe the mouse location? The rotation more times than not can be Quaternion.Identity, but read this for mouse position:
https://docs.unity3d.com/ScriptReference/Input-mousePosition.html
Lastly to destroy, you could use the destroy and pass a timer, run this in it's own script that simply checks a timer in an update, or use a coroutine and destroy the object, so more reading:
https://docs.unity3d.com/ScriptReference/Object.Destroy.html https://docs.unity3d.com/ScriptReference/Coroutine.html
I know you may have just hoped someone would write a script for you, but these are all fairly basic, but important building steps. It would be a shame for someone to take that away from you, so read, write some code, watch it most likely fail, read and write some more, and then once it finally works you will have something to be proud of.
I also just found this, which looks like a solid starting point for you:
https://unity3d.com/learn/tutorials/topics/scripting/instantiate
I would suggest spending time with those tutorials, especially for a beginner, as many of them are aimed specifically at beginners and take you through a lot of the basic functionality beginners struggle with.
Answer by Firedan1176 · Jul 28, 2016 at 01:55 AM
Coroutines run alongside your Update code, and they have the handy feature of WaitForSeconds, which I see was in your tags. Here's some code to get you started:
public GameObject obj;
public float waitTime = 2; //Wait 2 seconds after click to destroy
GameObject reference;
void Start() {
reference = (GameObject)Instantiate(obj);
}
void Update() {
if(Input.GetMouseButtonDown(0)) StartCoroutine(WaitThenDestroy());
}
IENumerator WaitThenDestroy() {
yield return new WaitForSeconds(waitTime);
Destroy(reference);
}
I know how it can be frustrating when you have no idea what some people are talking about, and it only makes you more frustrated. Let me know if this helps you. If you have any questions, just leave a comment!
That is because the correct spelling is IEnumerator .
Thanks. It doesn't have any errors now, but it doesn't work. I tried with multiple prefabs and they doesn't spawn correctly, and when they spawn they spawn in a random direction =P
Your answer
Follow this Question
Related Questions
How do I spawn an array of prefab clones as children of an object? 1 Answer
Adding or removing objects depending on height of parent object in 2D game 1 Answer
Want to instantiate Prefab in its parent. 1 Answer
Destroying Prefab Clone and replacing it with another 0 Answers
Instantiating prefab causes existing instances to change values 1 Answer