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
1
Question by DaveA · Jan 20, 2012 at 11:40 PM · arrayreferencecopy

(Yet another) Accessing array in another script - ref or copy?

I know how to access an array from another script. I'm pretty sure I know the answer to this, but want to be sure: am I accessing a copy of that array, or a reference to it? What if that array is changed?

Ex (both scripts on same object):

    class ClassA {
       Color32[] colors;
       void Start {
        colors = new Colors32[640*480];
       }
       Update() {
        // change colors
        foreach (c in colors)
          c = RandomColor();
        // or REALLY change colors
        colors = someTexture.GetPixels();
      }
    }

now in another class I want to get at the CURRENT 'colors' array.

 class ClassB {
  ClassA A; // will set with Inspector
  void Update() {
    Analyze (A.colors); // the CURRENT color array from object A
  }
 }

So, what will A.colors be in classB's Update? Ideally what I want is a reference to that array so I can read/write it, no matter what it happens to be, like a reference or C pointer-to-pointer.

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

Answer by Statement · Jan 21, 2012 at 12:48 AM

This code shouldn't do anything (at least not in C#), because c will become a temporary variable, so assigning a value to this temporary variable means it'll be lost.

 foreach (c in colors)
     c = RandomColor();

You are accessing a reference to the Color32 array. The contents are not copied by accessing it.

So, what will A.colors be in classB's Update?

A.colors will probably reference a new array each time as I suspect someTexture.GetPixels() return a new array each call. The copying doesn't happen when you access A.colors -- it'll just be a reference and the contents will be shared across different accesses. However, it's ClassA.Update that set a new reference to colors each update, and I am pretty sure each call to GetPixels generate a new array.

So the net effect is that you'll probably see different references each time you access ClassA.colors because it's reassigned every frame.

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 DaveA · Jan 21, 2012 at 03:59 AM 0
Share

Thanks. Yeah that random things was more of an indication of setting values within an existing array, vs (as you correctly say) getting a whole new array via GetPixels.

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

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I get a reference (not a copy) to a string from a script? 2 Answers

Scripts help 1 Answer

Reading the name of scripts, and creating an array? 1 Answer

Making a deep copy of gameobject. 1 Answer

Problem with copying array that holds Raycasthit objects 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