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 /
avatar image
0
Question by Kingdomkey6677 · May 23, 2020 at 09:18 PM · instantiatedestroytagrespawningfindgameobjectwithtag

Looking for gameObjects in the update function

Some Items are destroyable in my game. When they are destroyed, I want them to respawn, forcing there to always be at least one of those objects in the scene.

I want to do this by calling FindGameObjectWithTag in the update function and instantiating the object if it returns null.

However, something tells me that calling FindGameObjectWithTag on every frame might not be wise. Is there another function I should do this in? Or perhaps an entirely different, more optimized method to respawn objects?
Thanks in advance!

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by ShadyProductions · May 23, 2020 at 09:59 PM

Why don't you just re instantiate another of the item, after you call destroy on the item? That way you don't need to do anything in update.

Comment
Add comment · Show 2 · 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 Kingdomkey6677 · May 23, 2020 at 10:26 PM 0
Share

I can't say I'm entirely sure what you mean. How can I call Instantiate on an object that has been destroyed? Does the script finish running before the object destroys or will all lines after the destroy function be ignored?

Also, my objects are being dropped off an edge of sorts and being destroyed by a "death Barrier" trigger collider. In other words, the dropped object is being destroyed by another object. Will this get in the way of re instantiation? Perhaps I should have the object check its position and destroy itself when it drops below a certain y value. Would this be a better approach?

avatar image ShadyProductions Kingdomkey6677 · May 24, 2020 at 05:51 PM 0
Share

Why think so hard about this, you have a script that does something like this right?:

 public void OnCollisionEnter(Collision collision) 
 { // deathbarrier
     Destroy(collision.gameObject);
 }

all u have to do is instantiate a copy of the gameobject, or even beter, just reset the object ins$$anonymous$$d of destroy it, but that's up to you.

 public void OnCollisionEnter(Collision collision) 
 {
     var copy = Instantiate(collision.gameObject, new Vector3(0,0,0), Quaternion.identity);
     Destroy(collision.gameObject);
 }

Or KoenigX3 answer is also viable, if you only want to instantiate new objects if they fall below a certain threshold of active objects in scene.

avatar image
0

Answer by KoenigX3 · May 24, 2020 at 09:58 AM

You could create a DestroyableObjectManager class, which has the references to all the destroyable objects in your screen. This could be done by using a Dictionary with GameObject key and bool value. Also, it could be a good idea to create an integer variable in this class, which will store the number of currently active destroyable GameObjects.

Whenever you destroy an object, call a function of this class, in which you can set the reference of the GameObject to null and decrease the counter.

That way you can use the counter to check how many objects are currently active in the scene. If there isn't enough objects, you can check your dictionary, and re-instantiate some of the objects which are null (destroyed). The reason why we are using Dictionary here is that you can look for the GameObject using a reference, and you don't have to iterate through a List to find it.

Comment
Add comment · 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

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

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

Network.Destroy and collision; destroying correct object. 1 Answer

Problem with destroying instanced objects 1 Answer

Respawning gameobject 2 Answers

Can't seem to destroy instantiated objects. 1 Answer

How can I destroy an specific clone already instantiated, when there's more than one? 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