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 AnishsUsername · Apr 29, 2018 at 03:19 PM · destroyassetsspawningdatadestroy object

Destroying assets is not permitted to avoid data loss?

Hey guys,

I Have a Issue with destroying my Gameobject. I can't see the problem here because I'am using the Destroy() function like this every time.

I hope you can help me out :).

Source:

 // sets the point where the obstacle has to spawn
 Transform spawnpoint = spawnPoints[spawnIndex];
     // sets the rotation of the spawnpoint
 spawnPoints[spawnIndex].rotation= Quaternion.Euler(new Vector3(0f,0f,Random.Range(0f, 90f)));
     // Spawns the actual Obstacle
 Instantiate(obstacles[obstacleIndex], spawnpoint.position, spawnpoint.rotation);

     
     yield return new WaitForSeconds(5f);

     // Destroy the same Obstacle after 5s
     Destroy(obstacles[obstacleIndex]);
Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by Bunny83 · Apr 29, 2018 at 04:20 PM

Instantiate does not "spawn" the given object but it creates a duplicate of it in the scene. That newly created duplicate is returned by the Instantiate method. The object stored in the obstacles array is still the prefab in your project. You do not try to destroy the instance you've created but you try to destroy the source object, specifically the prefab of your project.


You have to do:

 var instance = Instantiate(obstacles[obstacleIndex], spawnpoint.position, spawnpoint.rotation);
 yield return new WaitForSeconds(5f);
 Destroy(instance);

Though this could even be simplified and handled without a coroutine all together since the Destroy method can take a second "delay" parameter. So you could just do

 Destroy(Instantiate(obstacles[obstacleIndex], spawnpoint.position, spawnpoint.rotation), 5);

and it will create that obstacle and will automatically destroy it after 5 seconds. Though if you plan to use an object pool in the future my first example would be the better one as it can be changed more easily to use an object pool instead of Instantiate and Destroy.

Comment
Add comment · Show 3 · 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 Owen-Reynolds · Apr 29, 2018 at 07:09 PM 0
Share

RE: only that 1st sentence: in the broader game design world, Spawn is a synonym for Instantiate. Those code comments seem fine to me.

But otherwise, yes, the OP should take another look at the manual.

avatar image Bunny83 Owen-Reynolds · Apr 30, 2018 at 02:30 PM 0
Share

Yes, spawn is an abstract term and loosly means "bring something into existence". Though depending on the context it can mean something different. Like in a networking environment "spawning" could only mean "activating". It's still the same concept of bringing it into existence but does not necessarily mean Instantiating / creating / cloning. The same holds true when you use object pools where you don't create and destroy objects but reuse them and only create / clone one if the pool is empty. To be a synonym it has to work in both directions. If you say Instantiate is equal to spawning it means spawning is equal to Instantiate which is not always the case. So you could say Instantiate is a more concrete example of spawning

avatar image Owen-Reynolds Owen-Reynolds · Apr 30, 2018 at 07:46 PM 0
Share

To be more clear, in older game engines Spawn(a) is the command that Unity renamed Instantiate(a).

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

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

Unity 5.3.1f1 Objects destroying on apk build but not in the editor 1 Answer

Spawn Object on Set Score 0 Answers

Respawn Ball to specific spawnpoint after death 1 Answer

My GameObject remains inGame but is destroyed in the editor (no GameObject on left side/in hierarchy) 0 Answers

making objects react to colliders without tag C# 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