Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by Zitoox · Jul 28, 2016 at 12:39 AM · prefabdestroyspawnbeginnerwaitforseconds

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...

Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Hanoble · Jul 28, 2016 at 01:10 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Zitoox · Jul 28, 2016 at 03:08 AM 0
Share

It seems to have a error with the IENumerator =/ alt text

error.png (2.6 kB)
avatar image andrei2699 Zitoox · Jul 28, 2016 at 05:16 AM 0
Share

That is because the correct spelling is IEnumerator .

avatar image Zitoox andrei2699 · Jul 29, 2016 at 05:29 PM 0
Share

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

@andrei2699 @Firedan1176

Show more comments

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

68 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges