Why cant i instantiate my ship? Error NullReferenceException: Object reference not set to an instance of an object.
Im sorry if its obvious, im pretty new to this. basically when the ship moves to far to the right or left it runs into warp or warp2. when it collides i want it to instantiate to the other warp. but i keep getting the Error NullReferenceException: Object reference not set to an instance of an object.
CODE:
var warp : GameObject;
var warp2 : GameObject;
var ship : GameObject;
function OnTriggerEnter2D(obj)
{
var name = obj.gameObject.name;
if(name == "barrier1"){
Destroy(gameObject);
Instantiate(ship, warp.position, Quaternion.identity);
}
if(name=="barrier"){
Destroy(gameObject);
Instantiate(ship, warp2.position, Quaternion.identity);
}
}
Try printing the names of ship, warp, and warp2 at the top of that function and see if any of them aren't assigned.
Answer by lorenzo17 · Oct 23, 2016 at 02:03 PM
Hi, I think you forgot to declare the collider type on the function OnTriggerEnter
It should be like this:
function OnTriggerEnter2D (other: Collider2D)
{
}
no im afraid thats not right. the "(other : Collider2D)" just gives me an error. im using the same "OnTriggerEnter2D (obj)" on my ship to take damage from spawn rock(clone)'s and its working fine.
Answer by phxvyper · Oct 24, 2016 at 09:59 PM
Make sure that ship
, warp
, and warp2
are being set and are not null. It is highly likely that one of these variables is null, hense the NullReferenceException. (Learn to read exceptions, they give you a lot of information)
Your answer
Follow this Question
Related Questions
NullReferenceException problems. 0 Answers
Bogus Null Reference in Build only,Bogus Phony NULL REFERENCE in BUILD ONLY (works in editor) 0 Answers
Null Reference Exception anytime scene is reloaded? 1 Answer
Getting error NullReferenceException on instantiate 3 Answers
Using TextMeshProUGUI variable in a static function is returning a NullReferenceException 0 Answers