I’m using unityscript. I’m starting to get the hang of breaking functionality down into separate dedicated scripts and calling GetComponent to find other scripts where necessary, or just using the drag and drop linking in the unity editor.
I’ve noticed that it’s probably good practice to check that references are not null before running code. I know how to do this by using an if/else statement and then containing the relevant code in brackets and providing a Debug.Log error message to myself. I was just wondering if there was a ‘neater’ way to do this that I might not be aware of, as it makes the code somewhat harder to read. If not, I guess I’ll stick with if/else statements.
I’d try breaking apart the script into functions perhaps. If a script requires a lot of references to be non-null in order to work you could have them all in one CheckReferences() function that returns true if successful.
Alternatively you could use a try-catch block within your script. If you miss a null reference, the instant your script tries to use it, the function quits and Unity catches the NullReferenceException. If you don’t want the whole function to break when this happens you can write your own special null reference handling. This is a C# feature but I presume UnityScript has something equivalent.