- Home /
NullReferenceException: Object reference not set to an instance of an object
Hello, i've got this compiler message but don't know how to solve it. It's about this part of my js code:
for(stein in steineliste){
stein.CreateStone();
}
stein is an object of class Stone which has the function CreateStone().
With this code used it works fine:
function CreateStone(){
stone = GameObject.CreatePrimitive(PrimitiveType.Cube);
stone.transform.position = Vector3(pos_X, pos_Y,0);
stone.transform.localScale += Vector3(width,height,0);
}
But as soon as I use my Prefab I get the compiler message mentioned in the title:
function CreateStone(){
stone = Object.Instantiate(UnityEngine.Resources.Load("CubePrefab"),Object.transform.position,Object.transform.rotation);
stone.transform.position = Vector3(pos_X, pos_Y,0);
stone.transform.localScale += Vector3(width,height,0);
}
First off, the message you're getting isn't from the compiler. Exceptions are run-time errors, they're thrown when errors occur after your program has passed compilation.
Second, a NullReferenceException happens when you're trying to use some object in your code that doesn't point to anything, i.e. it refers to null, a "NullReference". In this case, it's probably because Resources.Load failed to locate an asset with the name CubePrefab.
Object.transform.position doesn't look right either... Object doesn't have a member called "transform". That code shouldn't pass compilation.
Thx, I changed the Object.transform things to transform.position/rotation: BCE0005: $$anonymous$$ identifier: 'transform'. And Assets/Resources contains a CubePrefab.prefab. How can i check if it's loaded?
Follow aldonaletto's instructions below, and create a public Transform to hold the prefab, then drag it onto that in the editor.
Doesn't solve the problem :( $$anonymous$$aybe I am the problem :(
Take a look at my answer: I removed the first line of CreateStone, because it was creating a temporary prefab variable - and you wanted to use the public prefab.
Answer by aldonaletto · Sep 06, 2011 at 12:55 PM
The problem is that you're using the Object class instead of an actual instance to define position and rotation. I would also store the "CubePrefab" in a GameObject variable to define its type, like this:
function CreateStone(){ var prefab: GameObject = Resources.Load("CubePrefab"); // define the type as GameObject var stone = Instantiate(prefab, Vector3(pos_X, pos_Y,0), transform.rotation); stone.transform.localScale += Vector3(width,height,0); }
EDITED: If you created a public variable prefab outside the function to drag "CubePrefab" to, just remove the first line of CreateStone, like below:
public var prefab: GameObject; // <- drag your prefab here
function CreateStone(){ var stone = Instantiate(prefab, Vector3(pos_X, pos_Y,0), transform.rotation); stone.transform.localScale += Vector3(width,height,0); }
If i use this code, i get following warnings: Ambigous reference Resources $$anonymous$$ identifier: Instantiate $$anonymous$$ identifier: transform.
I even tried something like var prefab = "Resources/CubePrefab"; stone = Instantiate(prefab,...,...); Same problem again :(
$$anonymous$$aybe it's the problem that the code is written in a class which can't inherit from GameObject as it isn't possible to do something like class Stone extends GameObject?
ok, got it. Almost. i restarted unity and now i can drag the Prefab in the inspector to the public variable i created. BUT: If I do this, he doesn't know the identifier prefab in the CreateStone-function. Even if the public var prefab : GameObject has the same name. And if i create the public variable in the function as you did it in the example, i can't drag the Prefab in the instructor to this variable. Or if i copy the Resource.Load part, too, I still get the Warnings mentioned the answer before...
That's because you're still creating a prefab temp variable inside your function. Take a look at my answer above: I edited it to include this case.
Thx for your help again. I did exactly what you told me to do but it doesn't work: In the console I'm told that Instantiate is an unknown identifier and transform.rotation, too. If i add an Object. in front of Instantiate and transform, I still get this NullReferenceException. It seems to be right what you're telling me, that's not the point. But why doesn't it work? $$anonymous$$aybe because I do the Instantiate in a different class? The file has a function awake, a function which reads a xml file and two classes, one extends the other class. And I'm doing the Instantiate in the Subclass...
Im not sure about js as I use C#, but afaik you are not supposed to write js classes. If you do so you have to make sure your classes are derived from $$anonymous$$onoBehaviour otherwise you can't access things like transform or Instantiate. Also it's probably not possible or at least bad practice to have multiple classes in the same file with unity as unity requirese all components to be in a file that has the same name as the actual class. For JS you don't even need a class it will create that for you from the file.
Answer by StephanK · Sep 06, 2011 at 12:45 PM
Probably it can't find the prefab in the resources folder. Try declaring a public variable, assign your prefab to that variable from the inspector and use this instead of Resources.Load.
I tried this: public var prefab;
but can't assign the prefab to this variable in the inspector. What am I doing wrong?
I guess you have to tell unity what the type of the variable is. So try public var prefab : GameObject;