Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // t is a reference to a Transform.
- public Transform t ;
- private void Awake()
- {
- // If you do not assign something to t
- // (either from the Inspector or using GetComponent), t is null!
- t.Translate();
- }
- private void Awake ()
- {
- // Here, you try to get the Collider component attached to your gameobject
- Collider collider = gameObject.GetComponent<Collider>();
- // But, if you haven't any collider attached to your gameobject,
- // GetComponent won't find it and will return null, and you will get the exception.
- collider.enabled = false ;
- }
- private void Start()
- {
- // Here, you try to get a gameobject in your scene
- GameObject myGameObject = GameObject.Find("AGameObjectThatDoesntExist");
- // If no object with the EXACT name "AGameObjectThatDoesntExist" exist in your scene,
- // GameObject.Find will return null, and you will get the exception.
- myGameObject.name = "NullReferenceException";
- }
- var fov = Camera.main.fieldOfView;
- // main is null if no enabled cameras in the scene have the "MainCamera" tag.
- var selection = EventSystem.current.firstSelectedGameObject;
- // current is null if there's no active EventSystem in the scene.
- var target = RenderTexture.active.width;
- // active is null if the game is currently rendering straight to the window, not to a texture.
- private GameObject[] myObjects ; // Uninitialized array
- private void Start()
- {
- for( int i = 0 ; i < myObjects.Length ; ++i )
- Debug.Log( myObjects[i].name ) ;
- }
- delegate double MathAction(double num);
- // Regular method that matches signature:
- static double Double(double input)
- {
- return input * 2;
- }
- private void Awake()
- {
- MathAction ma ;
- // Because you haven't "assigned" any method to the delegate,
- // you will have a NullReferenceException
- ma(1) ;
- ma = Double ;
- // Here, the delegate "contains" the Double method and
- // won't throw an exception
- ma(1) ;
- }
- Collider collider = gameObject.GetComponent<Collider>();
- try
- {
- collider.enabled = false ;
- }
- catch (System.NullReferenceException exception) {
- Debug.LogError("Oops, there is no collider attached", this) ;
- }
- Collider collider = gameObject.GetComponent<Collider>();
- if(collider != null)
- {
- // You can safely manipulate the collider here
- collider.enabled = false;
- }
- else
- {
- Debug.LogError("Oops, there is no collider attached", this) ;
- }
- GameObject.Find("MyObject").GetComponent<MySuperComponent>().value = "foo" ;
- GameObject myObject = GameObject.Find("MyObject") ;
- Debug.Log( myObject ) ;
- MySuperComponent superComponent = myObject.GetComponent<MySuperComponent>() ;
- Debug.Log( superComponent ) ;
- superComponent.value = "foo" ;
- GameObject myObject = GameObject.Find("MyObject") ;
- if( myObject != null )
- {
- MySuperComponent superComponent = myObject.GetComponent<MySuperComponent>() ;
- if( superComponent != null )
- {
- superComponent.value = "foo" ;
- }
- else
- {
- Debug.Log("No SuperComponent found onMyObject!");
- }
- }
- else
- {
- Debug.Log("Can't find MyObject!", this ) ;
- }
- private GameObject gameObject;
- gameObject = new GameObject();
- [RequireComponent(typeof(Rigidbody))]
- public class AlwaysHasRigidbody : MonoBehaviour
- {
- Rigidbody myRigidbody;
- void Start()
- {
- myRigidbody = GetComponent<Rigidbody>();
- }
- }
- using System; // --> This exact line of code. That's it.
- using UnityEngine;
- public class Test : MonoBehaviour {
- public GameObject player; // --> Example to check if there's a null content;
- public void Update() {
- // You may now catch null reference here.
- try {
- player.transform.Translate(0, 0, 2);
- } catch(NullReferenceException e) { // --> You may use this type of exception class
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement