Setting Unity variables from Javascript in the browser.

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…

var Foo: String
var Bar: String = "Foo";
var Val: String = "Foobar";

// we want to set var Foo to equal "Foobar"
// but using Bar as the variable reference

// pseudo examples that are for clarity only..
*Bar = Val 
{&Bar} = Val;

Here’s an actual working example…

Browser JS:

function setVariable (key, val)
{
  var str = "myVariable=FooBar";
  GetUnity.SendMessage("MyObject", "setValue", str);
}

Unity JS:

var myVariable: String;
function setValue(arg: String)
{
  var args = arg.Split("="[0]);
  key = args[0];
  val = args[1];

  // key = String myVariable | val = String FooBar
  // how to reference variable of same name?
  key = val;
}

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.

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.