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 Chrizzle · May 02, 2011 at 07:57 AM · instantiatearraydestroytimingexecution

Destroy not destroying in time

Hey all, got another stupid question for you smart folks

I have "Cell" objects that destroy themselves when they run out of health, that part works fine.

I'm keeping a list of these objects in a global static array/"gameData" object in the scene. The function within the cell that destroys itself calls the gameData object's updateCells() function that updates the public list of all the existing cells in the scene. The problem is the cell isnt actually destroyed by the time updateCells is called, so it has a slot in the allCells array after it's been destroyed. I only call updateCells when a cell is instantiated or destroyed to save cycles, but currently the allCells array is inaccurate until another cell is born.

What's the best way to tell this gameData object that the cell is in the queue to be destroyed, so it isnt counted in the array of living cells?

For further confusion, the code works fine if the cell dies on it's own, but doesn't work properly when I shoot/kill the cell, and both occurrences call on the same exact code. The "natural" death is being calculated within update() and I guess being killed is somehow inbetween updates.

thanks all! -Chris

Comment
Add comment · Show 2
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 Cyb3rManiak · May 02, 2011 at 08:28 AM 0
Share

When you say you don't call updateCells() until a cell is destroyed - how exactly do you do it? Do you call Destroy() on it? DestroyImmediate()? Or is it a method call like Cell.DestroySelf()?

avatar image Chrizzle · May 02, 2011 at 08:18 PM 0
Share

In the cell I have a call killSelf() which calls Destroy(this), updates the score, then tells gameData to updateCells. The health change is being called by a custom collision component on the cell and bullets, and I think the collide component is calling cell.checkHealth() which calls killself() if it's health is zero or less

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by TheDemiurge · May 02, 2011 at 08:42 AM

Depending on what you're doing, how you're doing it and how you're calling this UpdateCells method of yours, the solution may vary. The easiest solutions would generally be to either:
(A) use MonoBehaviour.Invoke(..) which calls a function of your choice, and tell it to invoke your update method in, say, 0.2 sec or so,
(B) keep some kind of 'alive' flag on the cell and set it to false as you're destroying it, so that if the update occurs before it's fully destroyed it will be ignored/skipped/removed manually, or
(C) if you're using C# you can make an Event system using Delegates and Events, where your list, usually a Singleton-type manager object 'listens' for events and can register itself with every one of your objects (cells in this case), and as they die they will call their death method which will evoke the listener's OnDeath (or similar) method, which will tell it to remove said object from queue.

For more on C# events, just google C# delegates, and C# events, along with the word MSDN in the search, and you'll get right to the info and the example code. OR, if you say your system seems to work some of the time, you can try just debugging it a little to figure out why it may be failing other times, it might be something simple you overlooked.

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 Chrizzle · May 02, 2011 at 08:24 PM 0
Share

I'm using c#, but just learning it and unity at the moment. delegates and events sound a bit too advanced for me right now.

I was thinking about an invoke command, feels dirty but I really dont know the right way to do everything. I guess I could put an Alive check in updateCells (or just check the health) and not add any cells that should be dead. Co$$anonymous$$g from python, C#'s standard arrays bug me compared to a list, but that's what unity uses natively so I guess that's more efficient than a list object.

What's the best way to initialize a standard array of unknown length?

avatar image Chrizzle · May 02, 2011 at 08:26 PM 0
Share

I think it's succeeding on suicide because that health calculation happens during update and the Destroy() finishes at the end of update. The bullets are probably failing because they're detected by physics events and therefor are between updates.

avatar image TheDemiurge · May 03, 2011 at 06:56 AM 0
Share

The Wiki can help with this stuff far better than I can. http://www.unifycommunity.com/wiki/index.php?title=Which_$$anonymous$$ind_Of_Array_Or_Collection_Should_I_Use%3F As to what Unity uses natively, everything gets JIT compiled to C++ and such, so they're all pretty fast. But yes, things like built-in arrays are faster to process then Arrays and ArrayLists, but use what works best rather than going for tiny optimizations first.

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

Which is faster for a pool of objects, instantiating and adding to an array or loading pre-made objects to an array? 1 Answer

Prefabs into array than delete them when user interacts 1 Answer

Referencing an instantiated object with a global variable 0 Answers

How can I destroy objects and instantiates new in Array? 2 Answers

How to destroy an instantiated object when I touch it? 0 Answers


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