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?
There sure is, using our old friend Reflection. Try the following: Code (csharp): public int myVar = 42; void OnGUI () { if (GUILayout.Button("Output Variable Value")) { int newVar = (int)this.GetType().GetField("myVar").GetValue(this); Debug.Log("Variable value: " + newVar); } } If you want to access a variable in another object, simple change this to a reference to that object.
Sure, there's just a few minor syntax differences. Code (csharp): var myVar : int = 42; function OnGUI () { if (GUILayout.Button("Output Variable Value")) { var newVar : int = this.GetType().GetField("myVar").GetValue(this); Debug.Log("Variable value: " + newVar); } }
Hmm... I'm getting the following error: Code (csharp): NullReferenceException: Object reference not set to an instance of an object
Sorry for bumping this old thread but could anyone tell me how tonyd actually corrected the nullreference error ?
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.
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
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): string CurrentAnimation = "Run"; int Key = 1; void Start () { string HipsString = CurrentAnimation+"Key"+Key+"HipsRot"; Quaternion newVar = (Quaternion)this.GetType().GetField("HipsString").GetValue(this); }
Is the quaternion you're trying to reference a field, or is it a property? 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.
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): 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): using UnityEngine; using System.Collections; using System.Reflection; public class IKAnimation : MonoBehaviour { public string CurrentAnimation = "Run"; int Key = 1; Quaternion RunKey1HipsRot = Quaternion.Euler(0f, 0f, 340.2343f); void Start () { Quaternion newVar = (Quaternion)this.GetType().GetField(CurrentAnimation+"Key"+Key+"HipsRot").GetValue(this); Debug.Log("Variable value: " + newVar); } } Still the same error, thanks alot for at least trying to help me .
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).
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!
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?
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.