Hi,
I feel like this should be really simple, but a quick Google search turns up loads of people struggling with the same thing, and after reading everything I can find and trying out lots of stuff for hours, I still can’t get this simple thing to work - to be able to access one variable from another script on another object in a script on a different object.
Here’s the code:
@script ExecuteInEditMode;
var go;
go = GameObject.Find("Sensor");
var calibOn : int;
function Update () {
calibOn = go.OpenNISingleSkeletonController.calibratedOn;
}
So there’s an object called Sensor, with a script attached called OpenNISingleSkeletonController, with a public static variable called calibratedOn inside it.
On another object is the above script, which just seeks to find out the value of calibratedOn. The above doesn’t work, I ge the “not set to instance of Object” error on the final line.
What am I doing wrong?
Have been over and over the Unity documentation page and tried everything but just can’t make it work!
Thank you
S
If its a static variable you dont need an instance of the object to access it try:
calibOn = OpenNISingleSkeletonController.calibratedOn;
It says: “unknown identifier: ‘OpenNISingleSkeletonController’”
Have checked the spelling until my eyes hurt in case it was something simple like that. The above script is c-sharp and the one I’m trying to access it from is Javascript, is that a problem? Can they not share variables?
This is so frustrating, the whole next section of this project relies on being able to pass variables between objects/scripts!
Stuck!
It’s because “calibratedOn” variable is not a member of GameObject; it’s a member of the OpenNISingleSkeletonController script. So, what you have to do is use GetComponent something like this:
@script ExecuteInEditMode;
var go;
var openNiScript : OpenNISingleSkeletonController;
go = GameObject.Find("Sensor");
var calibOn : int;
function Update () {
openNiScript = go.GetComponent(OpenNISingleSkeletonController);
calibOn = openNiScript.calibratedOn;
}
Of course you should add error checking on this. You could do it all in one line of code, but it’s hard to debug, you can’t check errors, and I think in Unity 3.4 you can’t do that anymore.
Hope this helps!