- Home /
Null check madness >:(
I want to stop null reference exceptions. I just cant seem to get it today.
!= doesnt work either. I dont want to call setTarget if followPosition is a null value;
if(followPosition){
mover.setTarget(followPosition); //unity directs me to this line
}
Answer by RetepTrun · Apr 30, 2014 at 12:10 AM
ty, it went away when i added a check for that too
Answer by Owen-Reynolds · Apr 30, 2014 at 04:52 AM
An error about a null isn't like a typo. It means you're doing something you shouldn't -- like targeting a dead guy, or grabbing a "used" pickup, or trying to add a 4th life when you can only have 3. Somehow, you're trying to use something that isn't there.
The real way to fix them is to fix things so you won't use stuff that doesn't exist. Like when an enemy dies, take it off the list. Or when shooting at someone, think about what happens if this shot kills them (change targets?) Or, if a bullet gives the enemy credit for a hit, what should happen if they die and have a bullet in the air? Maybe all their bullets also vanish?
A program is like instructions. And any good set of instructions is naturally going to have "if it isn't there" clauses.