• 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 JULike · Nov 15, 2011 at 12:38 PM · javascriptarrays

Mutual exclusion Arrays and Destroy

Hi doods,

In my script multiple objects are getting and setting themselves from a static array and I'm thinking it should happen mutual exclusion, as per: http://forum.unity3d.com/threads/78983-Best-practice-for-an-action-system-with-mutual-exclusion?highlight=mutual+exclusion

Am I correct in thinking that the Destroy command removes the corresponding object reference from the array?

If so does this happen automatically under mutual exclusion? Or, should I manually remove the reference to ensure mutual exclusion?

Kind Regards

Comment
Add comment · Show 3
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 · Nov 15, 2011 at 03:32 PM 1
Share

The link seems broken. Goes to a question about StereoShaders for me.

avatar image Bunny83 · Nov 15, 2011 at 04:03 PM 0
Share

@owen-reynolds: Yep, it's broken.

avatar image JULike · Nov 15, 2011 at 07:38 PM 1
Share

It's funny coz I just copied and pasted the link: http://forum.unity3d.com/threads/78983-Best-practice-for-an-action-system-with-mutual-exclusion?highlight=mutual+exclusion

(fixed it above too)

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Nov 15, 2011 at 04:02 PM

I'm not sure what's your actual problem. First of all you should be more precise about what you want to do (maybe include a code fragment) AND what kind of "Array" you use. Native arrays can't be resized so you can't add or remove elements. If you talk about a List container or the UnityScript Array-class it's a different story.

Beside that i don't see any need for a mutex since Unity's scripting environment runs in a single thread so there are no concurrent scripts.

Unity's Destroy function is delayed until the end of the current Update().

Basically you can remove elements from a List at any time, except when you currently iterate through the collection. If you use a for-each loop it will throw an exception when you change the collection inside the loop. The easiest way to prevent such errors is to use a "normal" for loop and go backwards.

 // C#
 List<GameObject> myList;
 
 for (int i = myList.count-1; i >=0; i--)
 {
     // do something with myList[i]

     // It's safe to remove it from the list, since the indices before this item will stay the same
     if (ShouldBeDeleted)
     {
         Destroy(myList[i]);
         myList.RemoveAt(i);
     }
 }

The only thing you have to keep in mind is if other scripts holds a reference to an object that has been destroyed, it's reference will become "null". References in any kind of collections aren't removed automatically when they are null. If you think that an object can be destroyed from another script and you still hold a reference to it, make sure you check the reference against null before you use the reference:

 // C#
 public GameObject myObject;
 
 if (myObject != null)
 {
     // use myObject
 }
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 JULike · Nov 15, 2011 at 07:38 PM 1
Share

$$anonymous$$any thanks for such a comprehensive answer :)

I am using a native static 2D array [,] [x,y] to represent the x y axis of a 'connect 4' like game, each object (connect 4 piece), put's itself in the Array when it is in place. Then checks for null to see if there are other objects that would make a line then checks their colour then deletes the tiles.

static var tileArray : GameObject[,] = new GameObject[5,20];

Here's the part of the function that checks for lines:

for (var i=0; i <= Y; i++) {
if (tileArray[X, Y-i] != null){ downObject = tileArray[X, Y-i];} }

I'd add more code, but the formatting here makes it practically unreadable, I'm basically using a similar method as you describe above to iterate through the array check and destroy objects.

$$anonymous$$y code works great until a line is made such that the result is that multiple existing tiles fall at the same time. I was wondering if the concurrent access of the array by the multiple objects would be a problem. $$anonymous$$y concern is for access that performs a write function either putting the object in the array or deleting it and will when I delete it will there stil be a reference to it in the array.

If so I was thinking about using getters and setters to access this static array, and then enforcing mutual exclusion within these access functions. Is this a good idea and will I do it as per the above link in the comment above?

Any input greatly appreciated.

Thanks again :)

avatar image Bunny83 · Nov 16, 2011 at 12:16 AM 0
Share

In your case you shouldn't have any problems. As I said Untiy uses a single thread for the WHOLE scripting environment. All scripts and every possible code is executed from the same thread. It's impossible that two pieces of code run at the same time. However if you use coroutines (which also never run at the same time) it is possible that a variable reference is stored inside the coroutine and is carried over to a later point in time.

If you do a null-check right before you use the element, everything is fine.

A reference to an object will become null when it's destroyed but since the destroy action is delayed until the end of the current frame you should set it manually to null, so every other script knows it's destroyed / not available.

Btw. you can also post code in comments but you have to format it manually ;) Just an empty line before and after your code block and indent every line by 4 spaces, that's all ;)

 // This is a code example
 for (var i=0; i <= Y; i++)
 {
     if (tileArray[X, Y-i] != null)
     {
         downObject = tileArray[X, Y-i];
     }
 }
avatar image
0

Answer by JULike · Nov 16, 2011 at 10:58 AM

Great,

I had been doing a null check, so I'd put the funny behaviour after the Destroy statements down to concurrency issues. It's good to know it's not as complicated as that :)

Many Thanks,

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Getting C# to access a javascript global Array and Components 1 Answer

How would I call upon an asset in the projects tab with an array? 1 Answer

Converting a JS script into C# - hit a wall with arrays... 1 Answer

Remove Items and Item Tooltips 0 Answers

How do I delete the elements in array? 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