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

How to get variable name javascript?

Discussion in 'Scripting' started by macros2, May 4, 2014.

  1. macros2

    macros2

    Joined:
    Dec 16, 2013
    Posts:
    13
    var testint=1;

    var teststr="val";

    var val1=5;//print value

    var val2=8;

    var val3=4;

    Debug.Log(this[teststr+testint.ToString()]);//I want to variable name value.
     
  2. S3dition

    S3dition

    Joined:
    Jan 6, 2013
    Posts:
    252
  3. macros2

    macros2

    Joined:
    Dec 16, 2013
    Posts:
    13
    I'm going to use a lot of argument.If more had it not been for the if or switch.Like for instance cannot be similar?
     
  4. S3dition

    S3dition

    Joined:
    Jan 6, 2013
    Posts:
    252
    Sorry, I don't understand what you're asking. You shouldn't need to know the variable name in the debugger, as it already tells you the error and the line its on. Also, you shouldn't be debugging that many lines of code - just when there is an error. Even if you pass a lot of arguments, it will tell you which variable is null or cast improperly in the debugger without using debug.log.

    As for the rest, I'm not certain what you are asking, or what you mean by "If more had it not been for the "if" or switch.Like for instance cannot be similar?". Can you elaborate?
     
  5. macros2

    macros2

    Joined:
    Dec 16, 2013
    Posts:
    13
    As in my example I want to print the value of val1.
     
  6. S3dition

    S3dition

    Joined:
    Jan 6, 2013
    Posts:
    252
    OH!

    So you're trying to print the int of val1 as a string.

    Well, debug.log does it automatically:

    Debug.Log(var1);

    If you want to add them together, you can do:

    Code (csharp):
    1.  
    2. var varstr = value;
    3. var var1 = 2
    4. var testint = 3
    5.  
    6. Debug.Log(varstr + ": " + var1 + testint);
    7.  
    This gives you:

    Code (csharp):
    1.  
    2. Value: 23
    3.  
    However, if you want to print the value of an integer or float anywhere else, you would need to have .ToString() on the end of it.
     
    Last edited: May 4, 2014
  7. macros2

    macros2

    Joined:
    Dec 16, 2013
    Posts:
    13
    No, not that way.I want my (teststr + testint) in the form.Access the value of val1.
     
  8. S3dition

    S3dition

    Joined:
    Jan 6, 2013
    Posts:
    252
    Okay, I'm still not 100% understanding, so I'm going to shoot a few things out

    By form do you mean a text field: form1 = teststr + (val1 + testint).ToString();

    Or are you trying to add a string an int? var sum = val1 + testint + int.Parse(intstr);

    If those don't help you, then I can't understand what you're asking for.
     
  9. macros2

    macros2

    Joined:
    Dec 16, 2013
    Posts:
    13
    var testint=1;

    var teststr="val";

    var val1="val1 texts";//print

    var val2="val2 texts";

    var val3="val3 texts";

    Debug.Log(this[teststr+testint.ToString()]);//I want to val1.So "val1 texts"
    //////

    var testint=2;//!!!

    var teststr="val";

    var val1="val1 texts";

    var val2="val2 texts";//print

    var val3="val3 texts";

    Debug.Log(this[teststr+testint.ToString()]);//I want to val1.So "val2 texts"
    //////

    var testint=3;//!!!

    var teststr="val";

    var val1="val1 texts";

    var val2="val2 texts";

    var val3="val3 texts";//print

    Debug.Log(this[teststr+testint.ToString()]);//I want to val1.So "val3 texts"

    I don't want if (testint==1){Debug.Log(val1);} or switch(testint){case 1: Debug.Log(val1);} etc..
     
  10. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    You can use GetField on the GetType(this). Basically looking into your own object. So this will work:
    Code (csharp):
    1.  
    2. Debug.Log(this.GetType().GetField(teststr+testint).GetValue(this));
    3.  
     
  11. macros2

    macros2

    Joined:
    Dec 16, 2013
    Posts:
    13
    Thank you very much.
     
  12. S3dition

    S3dition

    Joined:
    Jan 6, 2013
    Posts:
    252
    Okay, so you want 1 debug.log for all 3 potential variables? Without any conditionals?

    I'm not certain you can. You need something there for the code to know which variable to output. How exactly would it know which variable you want debug.log to send you? Honestly, you could just do this:

    Debug.Log("Val1: " + val1 + " " + "Val2: " + val2 + " " + "Val3: " + val3);

    The only reason to use debug is when something bad happens, so either you need conditional formatting (if, switch, try/catch, etc) or you need to output everything and decide what is important, then comment out the debug.log line when you no longer need it.

    That's interesting.
     
  13. macros2

    macros2

    Joined:
    Dec 16, 2013
    Posts:
    13
    Thanks for help.zombiegorilla solved my problem.
     
  14. zombiegorilla

    zombiegorilla

    Moderator

    Joined:
    May 8, 2012
    Posts:
    8,952
    My guess was that the OP was just using the Debug as an example of what he was trying to do, not the actual/final implementation. Accessing variables dynamically isn't generally a best practice, but there can be some use cases for them.