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 Brice-Joly · Dec 16, 2012 at 12:21 AM · c#arrayboolean

How to circumvent this strange boolean array behaviour?

I'm trying to use a boolean array for some initialization and I ran into something I find rather misleading:

 bool boolA = false;
 bool boolB = boolA;
 boolB = true;
 Debug.Log(boolB + ", " + boolA); // Prints "True, False"
 
 bool[] arrayBoolA = new bool[] { false };
 bool[] arrayBoolB = arrayBoolA;
 arrayBoolB[0] = true;
 Debug.Log(arrayBoolB[0] + ", " + arrayBoolA[0]); // Prints "True, True"

For some reason in the second test, the boolean array seems to behave as if it was declared by reference, can someone clarify this point?

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 Dave-Carlile · Dec 16, 2012 at 12:33 AM

Array variables are reference types. When you execute arrayBoolB = arrayBoolA you're setting arrayBoolB to the same pointer/reference as arrayBoolA. So when you change the contents of either array they'll both have the same values because they're both referencing the same memory.

If you want a completely separate array you should be able to use Array.Copy to copy the contents into a different reference to the same type of array.

Comment
Add comment · Show 1 · 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 Brice-Joly · Dec 16, 2012 at 01:26 AM 0
Share

Thanks a lot Dave, Array.Copy did the trick. Come to think of it, I guess it makes sense since referencing is the default for "heavy" objects.

Nevertheless, I find it somewhat strange that in my code the array was defined as private set in a class, yet no error/warning was raised when the variable it was assigned to in another class was modified.

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

10 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

Related Questions

question about array or list of boolean 1 Answer

How to Spawn objects at a certain score. 1 Answer

multiple rows, in an array in C# 2 Answers

How can I do a foreach loop for an array of booleans? 1 Answer

Collecting Array items in order 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