I have a script running on my GameController which cycles through all the gameobjects of my scene, checks if they have a particular tag and if so calls a method on them.
SetUp() example (it’s very similar in every one of the different cases)
public void SetUp()
{
pickedUp = false;
// dirty way to create a persistent ID
iD = (transform.position.x.ToString() + transform.position.y.ToString()).Replace(",", "").Substring(0, 6);
}
The weird thing about this snippet of code is that, if i remove the try catch, i get a nullreferenceexception and the SetUp() method is never called.
However, if I add the try catch like in the example above, the code runs and the SetUp() method is called and works correctly… however i still get a log message in my console telling me about the same nullreferenceexception
Can anyone explain why this happens? I’m fairly new to c# so I might be missing something although after a little bit of research I am kind of at a loss for words.
Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.
You need to figure out HOW that variable is supposed to get its initial value. There are many ways in Unity. In order of likelihood, it might be ONE of the following:
drag it in using the inspector
code inside this script initializes it
some OTHER external code initializes it
? something else?
This is the kind of mindset and thinking process you need to bring to this problem:
I’ll look at all the links you provided me and will try to find out what’s null and how to fix it
However i’d like to ask you a question that is bugging me… if it is in fact a null item that I’m calling my method on, why does it work inside a try catch block? isn’t it supposed to be null no matter what?
also, it surely does not matter too too much but i’m working on a 2d project rn
Thanks that’s something I didn’t consider. So the code is failing at some point inside my method but keeps running and executes the stuff that shows me correct results. It does however still crash at some point. This makes sense
Yes absolutely, I’m looking into it right now but god save me ahah
Yea, your For loop is able to run through everything since its never stopped by an uncatched exception
most likely one of the objects in your scene has one of your switch-tags but not the associated component for this tag, while other objects do, so most likely the loop has trouble with one of the objects it loops through but not the others
you could just check which object this is by debug logging its name whenever the exception is catched
Just to make this extra clear; there are valid reasons to have try-catches into production ready code. Of course not to turn a blind eye on errors you may prevent by other means. But since there are some situations which may throw exceptions that are out of your hand to take care of otherwise. Imagine networking code connecting to some other party B… and then B goes off-line while you attempt to connect to them. So in that case the networking code may rightfully throw an exception, and you calling that code will have to watch out for these corner cases, as you dont want your program to crash just because an exception occured. That’s what try-catch is intended for.
“Try this, it should work” - “but it it does not, catch this exception and handle it as follows”
Yea of course, I wasnt trying to say that there is no point to them btw, if it came across like that^^
was just trying to say that they dont solve errors/ they dont fix them, errors dont go away because of them
i was trying to say that just because code is able to run with Try-Catch doesnt mean the errors are gone
I got why you said what you said, but i still thought it may be easy to misunderstand that they do have their uses based on what was said in the thread haha. I just thought it didnt hurt mentioning it to make sure