Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 Fabio Prevedelli · Sep 29, 2010 at 04:15 PM · destroyspawninstance

Destroy Istantiated objects after some time (different for each instance)

Greetings!! I'd like a suggestion on how to approach a problem i'm currently facing:

I'll outline the problem keeping everything else not relevant out of the window. Lets say i have two scripts: Player.cs Parts.cs

Player is unique, parts.cs is used by a Prefab and is used to spawn.. parts :)

Player have two int variables (CurrentSize and MaxSize) Player will spawn parts till MaxSize >= CurrentSize and the part spawned will have to be destroyed after "currentsize" seconds. After each spawn currentsize is increased and when a part is destroyed currentsize is decreased.

At this moment i am instantiating the prefab and in body.cs in the start function i have a reference to the player which will give me the currentsize and then launch a coroutine that will decrease the currentsize after x second and destroy the object.

What i believe is a better way to do it would be when from player.cs i instantiate with something like this:

Instantiate(BodyPrefab, transform.position, Quaternion.identity);

Store somehow a reference of the Objects i'm creating and destroying it from the player script (and also decreasing currentsize). To make it clear the parts i'm creating have all different time to live so i need to find the specific one i instantiate at that moment (another one created later have different time to live).

Hope i was clear enough! Any help is really appreciated:) Thanks Fabio

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
Best Answer

Answer by Albert · Sep 29, 2010 at 04:39 PM

If the parts' parent is the player, you could use the BroadcastMessage or SendMessageUpwards function to communicate between them.

You could add a function in your player.cs for adding/subtracting currentSize like:

public void changeSize(int amount)
{
   currentSize -= amount;
}

then in your part.cs, you can make something like this:

public void killSelf(float timeKill) { StartCoroutine(killNow(timeKill)); }

 public IEnumerator killNow(float timeKill)
 {
     yield return new WaitForSeconds(timeKill);
     SendMessageUpwards("changeSize", 20, SendMessageOptions.DontRequireReceiver);
     Destroy(gameObject);
 }

since you said that every instance will kill itself depents on the currentSize amount, so maybe something like this will work, add this where you instantiate the parts.

GameObject newPart = Instantiate(BodyPrefab, transform.position, Quaternion.identity);
newPart.SendMessage("killSelf", currentSize);
Comment
Add comment · Show 5 · 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 Fabio Prevedelli · Sep 29, 2010 at 05:50 PM 0
Share

Thank you for your input! Unfortunatly they are not parent/child classes. Would it be possible to store in an arraylist somehow the reference of the object being created and do all the destroying part from the player.cs script?

avatar image Fabio Prevedelli · Sep 29, 2010 at 06:03 PM 0
Share

Just for test i made it a child, it works except the most important part. The public void killnow method doesnt get invoked and in the log i see "trying to invoke method couldnt be called"

avatar image Albert · Sep 29, 2010 at 08:05 PM 0
Share

hello, i edited the script above, the part.cs area. i dont know what's wrong with the invoke, so i changed it to StartCoroutine.

avatar image Fabio Prevedelli · Sep 30, 2010 at 09:19 AM 0
Share

Greetings Clave! I'm giving you a thumb up for your help =) With the coroutine it does work. The sendmessageupwards doesn't but i worked around it by setting a reference to the player(which is unique) with the gameobjectfind and then accessing the currentsizevariable.

It now works, i still feel that it would be better to save a reference of the instance of the parts i create in the player (in an Arraylist) and destroy them from the player script after the condition is met (i.e. after a delay for instance.) but i don't know how to do it .. yet =)

avatar image Albert · Sep 30, 2010 at 02:48 PM 0
Share

yes, i think it's also possible using ArrayList, with that, you can manipulate everything in one script. goodluck :)

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

No one has followed this question yet.

Related Questions

A node in a childnode? 1 Answer

Instantiated object is destroyed in scene, but still logs as existing in console. 1 Answer

Level spawning statement help 0 Answers

Losing references after reloading the scene, even though they exist again. 1 Answer

Destroying and Loading Object Promblem? 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