- Home /
How do I access a script variable from a class defined within that script?
I'm pretty sure I'm being very dim here but I have a unity script which contains some script variables and also some classes. In the functions of one of the classes I want to access the script variables. I've simplified my script down to the following example that shows what I want to do:
#pragma strict
var testInt: int = 1;
class TestClass {
function testFunc() {
Debug.Log("testInt is " + testInt);
}
}
var testClass: TestClass;
function Awake() {
// Instantiate the class
testClass = new TestClass();
}
function Update () {
testClass.testFunc();
}
but I get the error "TestVarClass.js(6,35): BCE0005: Unknown identifier: 'testInt'."
Is there a simple way to access testInt inside TestClass? I realise I could pass it in as a parameter but this is not practical in my actual script that is more complex than the above. Thanks in advance.
This sounds similar to a question I asked a while ago http://answers.unity3d.com/questions/288589/call-function-outside-class-in-same-script.html
Thanks Linus, that page gave me some ideas. Thanks Jeff, I want the equivalent of testInt in my real script visible in the Unity Editor so I can tune it so I have it at the top level. There are also many instances of the class in an array (sorry, I stripped that bit out of my example) and I want a common value for each. It seems that parameter passing is the way to go to get the values into the class. I was trying to avoid this since in the actual script there are many values from the top level that I want to access but I just realised that I can make a little container class to hold all of them and then just pass that as a single parameter and dereference from that inside the class. I come from a block scoping background so it looked to me like it should have been simple to access but never $$anonymous$$d!
This is because UnityScript does some weird class wrapping. Your top level variable is inside a class, just not one you have declared. One of the advantages of C# is you have much less stuff being auto implemented by the compiler.
I realise this may not actually be helpful, but if you are jus starting out switching may be viable.
If you are after common variables you can use the static modifier from inside a class.
Answer by fafase · Nov 15, 2014 at 09:58 AM
One reason you would nest a class within another class is because you need to store data into a container for use within that class (Most of the time).
But the container should not be aware of the nesting class, it just doe its own thing. In here you TestClass trying to access a variable from the nesting class, though it may be possible via some hack, it also means you have a flaw in your design. A class is meant to do things about itself. So your printing should be in the parent class. If you need to access also info from the nested class then do it in the parent class:
class TestClass
{
var data:int;
}
var testClass:TestClass;
var info:int;
function Start()
{
info = 10;
testClass = new TestClass();
testClass.data = 20;
PrintOut();
}
function PrintOut()
{
Debug.Log(testClass.data + " " + info);
}
Thanks Fafase. As you suggest I have ended up using a container for the editor data in my actual script.