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 pateras · Jul 23, 2013 at 03:29 PM · referencevaluedictionarycopy

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

I'm trying to build a dictionary that contains a reference to a string from a script, such that if the string's value changes, so does the dictionary entry's value changes. I think I'm getting a copy somewhere along the line, and I'm not sure where.

 public class StringTest : MonoBehaviour
 {
     public string TheString = "old value";
 
     void Start()
     {
 
     }
 
     private void OnGUI()
     {
             if (GUI.Button(new Rect(200, 200, 200, 200), "Click string"))
             {
                 TheString = "Changed";
             }
     }
 
     void Update()
     {
 
     }
 }
 
 // in a test script
 Dictionary<string, string> parameters = new Dictionary<string, string>();
 parameters.Add("test_val", GameObject.Find("ValueHolder").GetComponent<StringTest>().TheString);
 
 private void Update()
 {
     if (parameters != null && parameters.Count > 0)
     {
         Utility.Log(parameters.First().Key + "-" + parameters.First().Value);
     }
 }

After I hit the button, TheString's value has changed (I can see it in the inspector), but the test script still prints "test_val - oldvalue".

Where is the copy taking place here, and how can I avoid it?

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
1

Answer by Bunny83 · Jul 23, 2013 at 04:18 PM

That's not possible because even a string by itself is actually a reference type but it's treated like a value type since it's an immutable type. So everytime you change a string you will get a new string which will be created somewhere else in memory.

So even when you have a reference to a string, you can never change the string. A string, once created, can never be changed again. See this SO question for more insight: http://stackoverflow.com/questions/10792603/how-are-strings-passed-in-net

What you can do is storing a class in your dictionary which has a string variable. The reference to the class instance can be passed around and you can "change" the string of that class instance (actually you're replacing the string with a new one).

In your case you can declare your Dictionary like this:

     Dictionary<string, StringTest> parameters /*...*/;

then do this:

     parameters.Add("test_val", GameObject.Find("ValueHolder").GetComponent<StringTest>());

In your Update loop you can use:

     parameters.First().Value.TheString
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
-1

Answer by BimSekai · Jul 23, 2013 at 03:42 PM

Have you tried the C# "ref" keyword ?

 Dictionnary <ref string , ref string>

(I can't test it, unfortunately...)

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 pateras · Jul 23, 2013 at 03:44 PM 0
Share

If you mean parameters.Add("test_valu", ref gameObject..."), then yes. It doesn't compile.

avatar image BimSekai · Jul 23, 2013 at 03:48 PM 0
Share

Two differents step, both in the declaration and in the Add.

 Dictionary<ref string, ref string> parameters = new Dictionary<ref string, ref string>();
 parameters.Add("test_val", ref GameObject.Find("ValueHolder").GetComponent<StringTest>().TheString);

avatar image Bunny83 · Jul 23, 2013 at 04:00 PM 0
Share

Sorry, but that doesn't make any sense ;)

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

17 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

Related Questions

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

Reference script on Behaviour is Missing, but IT'S NOT MISSING 0 Answers

Help on a Power-Up Sequence 1 Answer

Getting UnassignedReferenceException even though reference assigned 1 Answer

What's the best way to store a reference to a script asset? 2 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