Because we are accessing a game object that doesn’t exist the run-time gives us a NullReferenceException Null ChecksĪlthough it can be frustrating when this happens it just means the script needs to be more careful. On the next line (line 9) we use the go variable and try and print out the name of the game object it references. In this example there is no game object with that name, so the Find() function returns null. The code simply looks for a game object called “wibble”. (class-GameObject.html)See in (Glossary.html#GameObject) go = GameObject.Find("wibble") A GameObject's functionality is defined by the Components attached to it. _GameObject_ The fundamental object in Unity scenes, which can represent characters, props, scenery, cameras, waypoints, and more. In this example, the code is: //c# example This makes the Null Reference Exception easy to find and fix. Also, the message says that the exception happened inside the Start() function. This error message says that a NullReferenceException happened on line 10 of the script file Example.cs. The error message will look something like: NullReferenceException: Object reference not set to an instance of an objectĪt Example.Start () in /Unity/projects/nre/Assets/Example.cs:10 When you get a NullReferenceException in your code it means that you have forgotten to set a variable before using it. Hence, if you try and access the object that is being referenced and there isn’t one, you will get a NullReferenceException. Reference types default to null to indicate that they are not referencing any object. Reference variables in c# and JavaScript are similar in concept to pointers in C and C++. The run-time will tell you that you are trying to access an object, when the variable is null by issuing a NullReferenceException. If a reference variable isn’t referencing an object, then it’ll be treated as null. A NullReferenceException happens when you try to access a reference variable that isn’t referencing any object.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |