Search Unity

  1. How can our website serve you better? Give us your feedback. Take our survey and let us know.
    Dismiss Notice

Setting Unity variables from Javascript in the browser.

Discussion in 'Scripting' started by cdalexander, Aug 18, 2010.

  1. cdalexander

    cdalexander

    Joined:
    Nov 9, 2009
    Posts:
    73
    Hello Gurus.

    As the topic reads, I'm having a problem setting Unity variables from within a browser, but, in a slightly different way than what may be considered "normal".

    Here is an example of just Unity JS that would be the equiv of what I'm trying to do...
    Code (csharp):
    1.  
    2. var Foo: String
    3. var Bar: String = "Foo";
    4. var Val: String = "Foobar";
    5.  
    6. // we want to set var Foo to equal "Foobar"
    7. // but using Bar as the variable reference
    8.  
    9. // pseudo examples that are for clarity only..
    10. *Bar = Val
    11. {&Bar} = Val;
    12.  
    13.  
    Here's an actual working example...

    Browser JS:
    Code (csharp):
    1.  
    2. function setVariable (key, val)
    3. {
    4.   var str = "myVariable=FooBar";
    5.   GetUnity.SendMessage("MyObject", "setValue", str);
    6. }
    7.  
    Unity JS:
    Code (csharp):
    1.  
    2. var myVariable: String;
    3. function setValue(arg: String)
    4. {
    5.   var args = arg.Split("="[0]);
    6.   key = args[0];
    7.   val = args[1];
    8.  
    9.   // key = String myVariable | val = String FooBar
    10.   // how to reference variable of same name?
    11.   key = val;
    12. }
    13.  
    I'm sure there are other ways to set Unity variables and I'd like to know the method for those -- but I am very interested in knowing how to do it this way (if it can).

    Thanks.
     
  2. andeeeee

    andeeeee

    Joined:
    Jul 19, 2005
    Posts:
    8,768
    You can't access Unity JS variables by name as you can with other flavours of JS (at least not without an inordinate amount of effort). If you need to access values from a string key, you might find a Hashtable useful.
     
unityunity