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 nycucumber · Jul 02, 2019 at 11:47 PM · bugnullienumerable

avoid null when multiple classes are modifying the same thing

pardon me for my question line, i couldn't phase it better, if you know a better way to describe this problem, please let me know.


I am trying to simply my code here so i do not have to paste my thousands of lines of crap to you.

lets say I have three classes, some pseudo code here:

 class A : monobehaviour{
   Dictionary<Item, int> myDict;
 
   void Update(){
 
       foreach(var pair in myDict){
           //do something with the pair.key  and pair.value
       }
   }
 }
 
 class B : monobehaviour{
     List<Item> myList;
 
     void Update(){
         foreach(var i in myList){
             //do something with i
         }
     }
 
 }
 
 class Item : monobehaviour{
 
     List<Item> neighbors;
 
     void Update(){
 
         for(int i=0;i<neighbors.Count;i++){
             if( /* me and my neighbor is too close */)
             {
                 Destroy(neighbor);
             }
         }
 
     }
 
 }


as you can see, sometimes Item class would destroy other Item if certain condition is met, this will cause other classes to complain since some pair's key in class A 's myDict or the certain Item in myList from class B might be null.


right now i use a for loop to check if there is any item in the list/dict is null, and remove them before the list/dict is being used.

But i feel like this is dirty and hard to manage. This approach is guaranteed to work tho.


there are other two options in my mind but with this production project being exhibited right now i do not want to experiment too much.

  1. use a System.Action / Event , call it right after any Destroy() function is called. and other classes can listen to it and clean up their lists/dict/whatever

  2. use lock to prevent list being modified when it is being iterated over.


if you can point out the pros and cons of the three approaches, i would be very grateful.

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 Iarus · Jul 03, 2019 at 12:58 AM

I think MonoBehavior has a OnDestroyed event. You could register to this event in Start or Awake of class A and any time you add to the dictionary. Then in your event handler, simply unregister from the event and remove the item from the dictionary.

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
avatar image
0

Answer by Optrix · Jul 03, 2019 at 03:59 AM

@Iarus has a good answer to the question.


You can extend it out a little further. When the item is added to the list on 'A', you can add a MonoBehaviour that can be used to de-register the object should it ever be destroyed.


 class NotifyOnDestruction : monobehaviour{
      A ReferencedFrom;
  
    void OnDestroy()
    {
        if (ReferencedFrom != null) ReferencedFrom.RemoveFromList(gameObject);
    }
  }

This means that A gets called every time that an item in its list gets deleted.


When adding items to the list in A, you'll need the following...

 Item.AddComponent<NotifyOnDestruction>().ReferencedFrom = this;
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

124 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

Related Questions

LitJson null bug 0 Answers

String in array is null when checking length, help bug?! 2 Answers

Null reference exception 1 Answer

SplatMap error 1 Answer

Unity null check 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