Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Access variable by string name?

Discussion in 'Scripting' started by tonyd, Mar 2, 2010.

  1. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Let's say I have a variable named myVar.

    I want to call it using the string "myVar". Other than setting up hashtables ahead of time, does anyone know a way to do this?

    Is there a way to cast/convert "myVar" to myVar?
     
    Luis0413 and Dest8 like this.
  2. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    There sure is, using our old friend Reflection. Try the following:

    Code (csharp):
    1. public int myVar = 42;
    2.  
    3. void OnGUI () {
    4.     if (GUILayout.Button("Output Variable Value")) {
    5.         int newVar = (int)this.GetType().GetField("myVar").GetValue(this);
    6.         Debug.Log("Variable value: " + newVar);
    7.     }
    8. }
    If you want to access a variable in another object, simple change this to a reference to that object.
     
    Luis0413, ow3n, rockninja90 and 13 others like this.
  3. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Thanks! Do you know the jscript equivalent?
     
  4. matthewminer

    matthewminer

    Joined:
    Aug 29, 2005
    Posts:
    331
    Sure, there's just a few minor syntax differences.

    Code (csharp):
    1. var myVar : int = 42;
    2.  
    3. function OnGUI () {
    4.     if (GUILayout.Button("Output Variable Value")) {
    5.         var newVar : int = this.GetType().GetField("myVar").GetValue(this);
    6.         Debug.Log("Variable value: " + newVar);
    7.     }
    8. }
     
  5. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Hmm... I'm getting the following error:

    Code (csharp):
    1. NullReferenceException: Object reference not set to an instance of an object
     
  6. tonyd

    tonyd

    Joined:
    Jun 2, 2009
    Posts:
    1,224
    Never mind, it's working now.

    Thanks again!
     
  7. Eideren

    Eideren

    Joined:
    Aug 20, 2013
    Posts:
    308
    Sorry for bumping this old thread but could anyone tell me how tonyd actually corrected the nullreference error ?
     
    SmartCatProduction likes this.
  8. TournyMasterBot

    TournyMasterBot

    Joined:
    Sep 8, 2013
    Posts:
    13
    He probably initialized the object to a value (string myVar = "some data")

    ... But you should probably make a new thread instead of necroing one from four years ago.
     
  9. Eideren

    Eideren

    Joined:
    Aug 20, 2013
    Posts:
    308
    Sorry, is it too late now ?

    I am trying to call a quaternion by using a bunch of string's value to recreate the quaternion variable's name, is that the correct way to do it ?
    i.e.: the quaternion is called RunKey1HipsRot,
    the string used are Animation+"Key"+IntKey+Bodypart+Type
     
  10. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    I think he read the docs. ;)

    Reflection in the .NET Framework
     
  11. Eideren

    Eideren

    Joined:
    Aug 20, 2013
    Posts:
    308
    It's unfortunately a maze for anyone who doesn't know what he should look at.
    "using System.Reflection" on top of the script did the trick but i still dont know how i should proceed.
    doing this just gives me the same null error at the Quaternion newVar line.

    Code (csharp):
    1.  
    2. string CurrentAnimation = "Run";
    3. int Key = 1;
    4.  
    5. void Start () {
    6.  string HipsString = CurrentAnimation+"Key"+Key+"HipsRot";
    7.  Quaternion newVar = (Quaternion)this.GetType().GetField("HipsString").GetValue(this);
    8. }
    9.  
     
  12. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    1. Is the quaternion you're trying to reference a field, or is it a property?
    2. Is the quaternion you're trying to reference a field / property of the class that has the Update() function?

    Edit:

    Also, your current code is searching for a field named "HipsString", not a field with the name in the HipsString var.
     
    Last edited: Apr 11, 2014
  13. Eideren

    Eideren

    Joined:
    Aug 20, 2013
    Posts:
    308
    It's a field : Quaternion RunKey1HipsRot = Quaternion.Euler(0f, 0f, 340.2343f);
    Ill use it in a Slerp, that's why it isn't a vector3.
    I dont even do anything besides the Start() yet.

    i didn't really know if
    Code (csharp):
    1. Quaternion newVar = (Quaternion)this.GetType().GetField(CurrentAnimation+"Key"+Key+"HipsRot").GetValue(this);
    would work and since i had the same error i thought that it didn't.
    Even with that i have the same error.

    That's the whole script
    Code (csharp):
    1.  
    2. using UnityEngine;
    3. using System.Collections;
    4. using System.Reflection;
    5.  
    6. public class IKAnimation : MonoBehaviour {
    7.  public string CurrentAnimation = "Run";
    8.  int Key = 1;
    9.  Quaternion RunKey1HipsRot = Quaternion.Euler(0f, 0f, 340.2343f);
    10.  void Start () {
    11.   Quaternion newVar = (Quaternion)this.GetType().GetField(CurrentAnimation+"Key"+Key+"HipsRot").GetValue(this);
    12.   Debug.Log("Variable value: " + newVar);
    13.  }
    14. }
    15.  
    Still the same error, thanks alot for at least trying to help me :) .
     
  14. Smooth-P

    Smooth-P

    Joined:
    Sep 15, 2012
    Posts:
    214
    GetField(String) only finds public fields.

    You can use reflection to find private fields, but you'll need to look at the docs for GetField(String, BindingFlags).
     
    Last edited: Apr 11, 2014
  15. Eideren

    Eideren

    Joined:
    Aug 20, 2013
    Posts:
    308
    Thanks a lot, that worked !
     
  16. tormentoarmagedoom

    tormentoarmagedoom

    Joined:
    Jun 17, 2016
    Posts:
    4
    Hello SmoothP. I see you know how to solve this. Its the same, but i dont get how i have to do it... :(
    i did a post asking almost the same. can you help me please?

    http://answers.unity3d.com/question...g-other-variables-values.html#comment-1204626

    Thanks a lot!
     
  17. msakkuzu

    msakkuzu

    Joined:
    Dec 5, 2013
    Posts:
    5
    What about if I would like to get a string from an array?
    string[] myVar={"a","b","c"};
    How do I use this.GetType().GetField thing?
     
    Luis0413 likes this.
  18. bboytcry

    bboytcry

    Joined:
    Nov 20, 2017
    Posts:
    1
    I Only Entered Here, to say you: Thanks!!!! For Sharing that Method, it worked me perfectly to asign a Sprite Value from a String name. Me ahorraste muchísimo código! saludos desde República Dominicana.
     
  19. yaroslav_

    yaroslav_

    Joined:
    Jul 12, 2018
    Posts:
    1
    this["myVar"]
    have you tried something like this?
     
  20. tanvas

    tanvas

    Joined:
    May 10, 2018
    Posts:
    1
    @yaroslav_ It says, that Type "*file name* (in my case) Inventory.js" does not supports slicing.
     
  21. pitibonom

    pitibonom

    Joined:
    Aug 17, 2010
    Posts:
    188
    wow !!!!
    this is caviar !!!!

    Thanks for the clue :)