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 Nyhtian · Feb 26, 2020 at 03:19 AM · arrayvariablereferencecustom-class

Reading reference variable alters said reference variable

So here's the method:

     public Act[] Orientate(ActionType type, ActionDirection direction)
     {
         Act[] orientatedAct = referenceActions[(int)type].acts;
 
         foreach (Act _act in orientatedAct)
         {
             Vector2 rotatedOrigin = new Vector2(_act.origin.x, _act.origin.y);
             if (_act.origin.x > -2)
                rotatedOrigin = Quaternion.Euler(0, 0, (90 * -(int)direction)) * new Vector2(_act.origin.x, _act.origin.y);
             Vector2 rotatedDirection = Quaternion.Euler(0, 0, (90 * -(int)direction)) * new Vector2(_act.direction.x, _act.direction.y);
             _act.origin = new Vector2Int(Mathf.RoundToInt(rotatedOrigin.x), Mathf.RoundToInt(rotatedOrigin.y));
             _act.direction = new Vector2Int(Mathf.RoundToInt(rotatedDirection.x), Mathf.RoundToInt(rotatedDirection.y));
         }
         return orientatedAct;
     }


...and for some reason of another, each time I call it, it alters a value within the referenceActions array. referenceActions is a serialized array used for me to define what the actions are. I don't reference referenceActions anywhere else, so I'm stumped as to why it's being changed. It seems to apply the rotation to referenceActions[(int)type].acts[0].direction.

Any help is appreciated. Happy to provide more details.

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 Cornelis-de-Jager · Feb 26, 2020 at 06:29 AM

So what is happening is --> you are creating a new variable, BUT, you are assigning the new variable to the same memory location as the old one. Meaning when you do this:

  Act[] orientatedAct = referenceActions[(int)type].acts;

they are actually sharing memory locations even if they are not the same variable. For example look at the following little program (you can test in https://dotnetfiddle.net/):

 public static void Main()
     {
         int[] test = new int[]{ 1, 2, 3};
         int[] test2 = test;
         test2[0] = 5;        
         
         foreach (var item in test)
             Console.WriteLine(item);
         //Output --> 5
         //Output -->  2
         //Output -->  3
     }

And that is what is happening to yours. To overcome this you need to create a variable with its own memory space. This can be easily done throught the Clone function:

 Act[] orientatedAct = (Act[]) referenceActions[(int)type].acts.Clone();

Hope this helped.

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 Nyhtian · Feb 26, 2020 at 06:50 AM 0
Share

Perfect! Thank you so much!

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

144 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 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

Editting a custom javascript class in the inspector 1 Answer

When using an Array i get a NullReferenceException 2 Answers

Variable name using another variable 2 Answers

.getComponent() creates an Instance? 1 Answer

Creating a Prefab and Keeping Original Objects Script Information 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