- Home /
How does UnityScript class scope work, when in same file?
If I have a script attached to my Player
object, PlayerControl.js
, I know that this will create a class called PlayerControl
. But let's say I made a class inside that script:
class Boundaries
{
public var something:float = 3.0f;
}
Then, when I try access that class from, say the FixedUpdate()
method:
public function FixedUpdate()
{
var boundary:Boundary;
//do something with boundary.something here
}
I will get this error:
NullReferenceException: Object reference not set to an instance of an object
PlayerController.FixedUpdate () (at Assets/Scripts/PlayerController.js:23)
However if I make the declaration outside the FixedUpdate()
method, it will work as expected.
So my actual question: How does the scope of classes work here? Obviously the variable is available inside the method, but the class doesn't seem to be. Is there another file being made in the background, or, more likely, is it a scoping issue I need to understand? This is actually something I've read how to do from the official Unity Space Shooter tutorial - so presumably this kind of class in a class paradigm is encouraged?