- Home /
Platfomer 2d game block destroying raycasting nullreference exception problem
Hi, I need a bit of help.
I generate a level and have destroyable blocks in it as well as ai characters that are set up with raycasts onto the solids layer to check different things, like if there is a platform next to them then they can walk left or if not walk right.
I've since added the ability to destroy the solids. (platforms and wall blocks).
I get null reference exceptions now from the characters raycasts once a blocks been destroyed (I'm pretty sure that's whats causing it). I've tried real hard to solve this but now don't know what to do. My raycast script is:
isPlatLeft = Physics2D.Raycast (new Vector2 (transform.position.x - 1.5f, transform.position.y), new Vector2 (-0.5f, -1), 8f, moveMask);
I've got just the walls and platforms and ladders set on the moveMask.
This is where I get the nullreference error:
// check if a platform is under the player and off to the left.
if (laddCheckLeft) {
if (laddCheckLeft.collider.tag != null) {
if (laddCheckLeft.transform.tag == "PLATFORM" || laddCheckLeft.transform.tag == "LaddCollider" || laddCheckLeft.transform.tag == "SOLID") {
// int rane = Random.Range (0, 40);
nowOffLadder = false;
//if (rane == 1) {
laddPlatToLeft = true;
//}
} else {
wasOnLadder = true;
laddPlatToLeft = false;
}
} else {
wasOnLadder = true;
laddPlatToLeft = false;
}
} else {
wasOnLadder = true;
laddPlatToLeft = false;
}
Everything worked fine until I started destroying blocks. As you can see I've tried to stop it from happening but not sure what to do now and whats going on.
Can someone help PLEASE???
Which line causes the NullReferenceException ?
(please, when you post a code, try to have a correct indentation and remove commented code if it is useless)
This does:
if (laddCheckLeft.transform.tag == "PLATFOR$$anonymous$$" || laddCheckLeft.transform.tag == "LaddCollider" || laddCheckLeft.transform.tag == "SOLID") {
Sorry bout the comments, but what do you mean about correct indentation, I pasted it as is from my code lol.
and what is laddCheckLeft ? can't see where it is initialized
I mean that by correct identation
if (laddCheckLeft)
{
if (laddCheckLeft.collider.tag != null)
{
if (laddCheckLeft.transform.tag == "PLATFOR$$anonymous$$" ||
laddCheckLeft.transform.tag == "LaddCollider" ||
laddCheckLeft.transform.tag == "SOLID")
{
// int rane = Random.Range (0, 40);
nowOffLadder = false;
//if (rane == 1) {
laddPlatToLeft = true;
//}
}
else
{
wasOnLadder = true;
laddPlatToLeft = false;
}
}
else
{
wasOnLadder = true;
laddPlatToLeft = false;
}
}
else
{
wasOnLadder = true;
laddPlatToLeft = false;
}
Answer by InvincibleCat · Jan 23, 2015 at 10:46 PM
This should work
if (laddCheckLeft)
{
Collider2D collider = laddCheckLeft.collider;
if(collider != null)
{
string tag = laddCheckLeft.collider.tag;
if (tag == "PLATFORM" ||
tag == "LaddCollider" ||
ltag == "SOLID")
{
// int rane = Random.Range (0, 40);
nowOffLadder = false;
//if (rane == 1) {
laddPlatToLeft = true;
//}
}
else
{
wasOnLadder = true;
laddPlatToLeft = false;
}
}
else
{
wasOnLadder = true;
laddPlatToLeft = false;
}
}
else
{
wasOnLadder = true;
laddPlatToLeft = false;
}
Thanks, it seems to work. Would have taken me ages to work that out by myself, I've not been coding that long. Awesome so I can have destroyable blocks coolies. Thanks again!
You're welcome ;) Don't forget to accept the answer!
I'll test it a bit more. I had to replace several code parts with the new script so it took me a few $$anonymous$$utes. But it seems to work. I have randomly generated levels that generate platforms and ladders and rooms in different directions and I want the characters ai to be able to do what you can, and so far its going really well, thanks to my perseverance and the wonderful people here on Unity Answers.
Glad that you found the solution.
Please don't forget to accept the answer. I spend to many time going after people asking them to accept so I can sort the questions I'm answering. And it is a real waste of time :(