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 stevethorne · Oct 23, 2014 at 03:19 PM · c#arrayreferencevalue

Store reference to array as variable

I want to have a class that can store references to arrays in a different class and be able to modify those arrays by the new arrays.

 public class test : MonoBehaviour
 {
     // Variables
     //,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,
     
     test2 testing;
     int[] original;
 
     void Start()
     {
         original = new int[] { 1, 2, 3, 4 };
 
         testing = new test2();
         testing.reference = original;
         testing.Change(); // changes original values
         testing.Modify(); // does not modify original
 
         DebugArray( original );
         Debug.Log( ":::" );
         DebugArray( testing.reference );
     }
 
     void DebugArray( int[] array )
     {
         for ( int i = 0; i < array.Length; ++i )
         {
             Debug.Log( array[i] );
         }
     }
 }
 
 public class test2
 {
     public int[] reference;
 
     public void Change()
     {
         reference[0] = 135134;
     }
 
     public void Modify()
     {
         reference = new int[] { 4, 3, 2, 1, 0 };
     }
 }


The problem I'm running into with this code is that I can change the values of the original array via the new class, but I can't modify the array. Is there a way I could do this in C#? It seems like a serious limitation of C# if I can't. Are there any alternatives? Maybe using IntPtr? To be clear I need to be able to change the size of the array in the new class as well as the values.

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
3
Best Answer

Answer by Habitablaba · Oct 24, 2014 at 12:00 AM

The right answer would be to think real hard about what it is you're trying to do, and then think real hard about a better way to do it.

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 Jeff-Kesselman · Oct 23, 2014 at 03:27 PM

To change the array referenced by a variable you need access to that variable. This is tru in C or C# or any other language.

 public class test : MonoBehaviour
  {
      // Variables
      //,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,.;.,
      
      test2 testing;
      public int[] original;
  
      void Start()
      {
          original = new int[] { 1, 2, 3, 4 };
  
          testing = new test2();
          testing.reference = original;
          testing.Change(); // changes original values
          testing.Modify(); // does not modify original
  
          DebugArray( original );
          Debug.Log( ":::" );
          DebugArray( testing.reference );
      }
  
      void DebugArray( int[] array )
      {
          for ( int i = 0; i < array.Length; ++i )
          {
              Debug.Log( array[i] );
          }
      }
  }
  
  public class test2
  {
      test1 theOtherInstance;
  
      public void Change()
      {
          theOtherInstance.original[0] = 135134;
      }
  
      public void Modify()
      {
         theOtherInstance.original = new int[] { 4, 3, 2, 1, 0 };
      }
  }

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 stevethorne · Oct 23, 2014 at 04:32 PM 0
Share

well in c++ i would just use a pointer and wouldn't have this problem

avatar image Kiwasi · Oct 23, 2014 at 06:40 PM 0
Share

Then use c++. Nothing is stopping you.

avatar image Jeff-Kesselman · Oct 23, 2014 at 11:49 PM 0
Share

Yes you would have this problem.

There is very little real difference between a pointer and a reference under the hood.

If you change one pointer to an array it does NOT change any other pointers to the same array-- they still point to the old data.

You would need a pointer to the pointer you want to change in order to change what it points at, and thats exactly what an object instance reference is.

C# is a true OOP language. If you desperately want to write pointer based procedural C code then mark your code as "unsafe" and do just that.

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

29 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

Related Questions

does Array.Concat() and Array.Push() add objects by Value rather than by Reference ? 1 Answer

How to check if vector in array was not changed? 1 Answer

How to let multiple values contribute two one value constantly 1 Answer

[c#] Save an instance for future re-instantiation 0 Answers

c# How does an == decide if two objects are the same? 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