when i try to assign a variable inside a class, using the following code:
static var Player : PlayerVariables;
class PlayerVariables
{
var PlayerObj : GameObject;
}
Start()
{
Player.PlayerObj = GameObject.Find("Player"); // there is a object in the scene called Player
}
i get a NullReferenceException: Object reference not set to an instance of an object
Since GameObject.Find() will not return an error if no object was found, this is not causing the error.
Since assigning any variable, even a null one, to null will not cause an error, .PlayerObj is not causing the error.
The only thing that could be going wrong is trying to do Player.PlayerObj. In other words, the variable Player is null! To double check this, insert Debug.Log("Player is null? " + ((Player == null)?“YES” : “NO”)); directly before “Player.PlayerObj =…”. Check your debug console for the result.
EDIT: I must have misread your code or something lol. Of COURSE Player is null, you never initialize it.
player = new PlayerVariables(); needs to be put in your Start function (Thanks, syclamoth!)
I'm not Familiar with js but mabye you can code like this:
static var Player : PlayerVariables = new PlayerVariables();
class PlayerVariables
{
var PlayerObj : GameObject;
}
Start()
{
Player.PlayerObj = GameObject.Find("Player");
// there must be an object in the scene called Player
}