Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. // t is a reference to a Transform.
  2. public Transform t ;
  3.  
  4. private void Awake()
  5. {
  6. // If you do not assign something to t
  7. // (either from the Inspector or using GetComponent), t is null!
  8. t.Translate();
  9. }
  10.  
  11. private void Awake ()
  12. {
  13. // Here, you try to get the Collider component attached to your gameobject
  14. Collider collider = gameObject.GetComponent<Collider>();
  15.  
  16. // But, if you haven't any collider attached to your gameobject,
  17. // GetComponent won't find it and will return null, and you will get the exception.
  18. collider.enabled = false ;
  19. }
  20.  
  21. private void Start()
  22. {
  23. // Here, you try to get a gameobject in your scene
  24. GameObject myGameObject = GameObject.Find("AGameObjectThatDoesntExist");
  25.  
  26. // If no object with the EXACT name "AGameObjectThatDoesntExist" exist in your scene,
  27. // GameObject.Find will return null, and you will get the exception.
  28. myGameObject.name = "NullReferenceException";
  29. }
  30.  
  31. var fov = Camera.main.fieldOfView;
  32. // main is null if no enabled cameras in the scene have the "MainCamera" tag.
  33.  
  34. var selection = EventSystem.current.firstSelectedGameObject;
  35. // current is null if there's no active EventSystem in the scene.
  36.  
  37. var target = RenderTexture.active.width;
  38. // active is null if the game is currently rendering straight to the window, not to a texture.
  39.  
  40. private GameObject[] myObjects ; // Uninitialized array
  41.  
  42. private void Start()
  43. {
  44. for( int i = 0 ; i < myObjects.Length ; ++i )
  45. Debug.Log( myObjects[i].name ) ;
  46. }
  47.  
  48. delegate double MathAction(double num);
  49.  
  50. // Regular method that matches signature:
  51. static double Double(double input)
  52. {
  53. return input * 2;
  54. }
  55.  
  56. private void Awake()
  57. {
  58. MathAction ma ;
  59.  
  60. // Because you haven't "assigned" any method to the delegate,
  61. // you will have a NullReferenceException
  62. ma(1) ;
  63.  
  64. ma = Double ;
  65.  
  66. // Here, the delegate "contains" the Double method and
  67. // won't throw an exception
  68. ma(1) ;
  69. }
  70.  
  71. Collider collider = gameObject.GetComponent<Collider>();
  72.  
  73. try
  74. {
  75. collider.enabled = false ;
  76. }
  77. catch (System.NullReferenceException exception) {
  78. Debug.LogError("Oops, there is no collider attached", this) ;
  79. }
  80.  
  81. Collider collider = gameObject.GetComponent<Collider>();
  82.  
  83. if(collider != null)
  84. {
  85. // You can safely manipulate the collider here
  86. collider.enabled = false;
  87. }
  88. else
  89. {
  90. Debug.LogError("Oops, there is no collider attached", this) ;
  91. }
  92.  
  93. GameObject.Find("MyObject").GetComponent<MySuperComponent>().value = "foo" ;
  94.  
  95. GameObject myObject = GameObject.Find("MyObject") ;
  96.  
  97. Debug.Log( myObject ) ;
  98.  
  99. MySuperComponent superComponent = myObject.GetComponent<MySuperComponent>() ;
  100.  
  101. Debug.Log( superComponent ) ;
  102.  
  103. superComponent.value = "foo" ;
  104.  
  105. GameObject myObject = GameObject.Find("MyObject") ;
  106.  
  107. if( myObject != null )
  108. {
  109. MySuperComponent superComponent = myObject.GetComponent<MySuperComponent>() ;
  110. if( superComponent != null )
  111. {
  112. superComponent.value = "foo" ;
  113. }
  114. else
  115. {
  116. Debug.Log("No SuperComponent found onMyObject!");
  117. }
  118. }
  119. else
  120. {
  121. Debug.Log("Can't find MyObject!", this ) ;
  122. }
  123.  
  124. private GameObject gameObject;
  125.  
  126. gameObject = new GameObject();
  127.  
  128. [RequireComponent(typeof(Rigidbody))]
  129. public class AlwaysHasRigidbody : MonoBehaviour
  130. {
  131. Rigidbody myRigidbody;
  132.  
  133.  
  134. void Start()
  135. {
  136. myRigidbody = GetComponent<Rigidbody>();
  137. }
  138. }
  139.  
  140. using System; // --> This exact line of code. That's it.
  141. using UnityEngine;
  142.  
  143. public class Test : MonoBehaviour {
  144.  
  145. public GameObject player; // --> Example to check if there's a null content;
  146.  
  147. public void Update() {
  148.  
  149. // You may now catch null reference here.
  150. try {
  151.  
  152. player.transform.Translate(0, 0, 2);
  153.  
  154. } catch(NullReferenceException e) { // --> You may use this type of exception class
  155.  
  156. }
  157.  
  158. }
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement