- Home /
NullReferenceException: Object reference not set to an instance of an object UnityEngine.GUI.DrawTexture
I need a character to equip a gasmask as soon as he walks into a posioned area. The image has to be across the entire screen. The gasmask GUI is the part that doesn't work. Everything else is fine. Line 23.
 #pragma strict
 
 public var MaskGUI : Texture;
 private var player : GameObject;                    // Reference to the player GameObject.
 private var playerInventory : PlayerInventory;  // Reference to the PlayerInventory script.
 public var GasmaskGrab : AudioClip;                         // Audioclip to play when the mask is picked up.
 public var GasPoisoning : AudioClip;
 
 function Awake ()
 {
     // References.
     player = GameObject.FindGameObjectWithTag(Tags.player);
     playerInventory = player.GetComponent(PlayerInventory);
 }
 
 
 function OnTriggerEnter (other : Collider)
 {
         if(other.gameObject.name == "Player"){
         // ... if the player has the mask...
         if(playerInventory.hasMask){
                 AudioSource.PlayClipAtPoint(GasmaskGrab, transform.position);
                 GUI.DrawTexture(Rect(10,10,60,60), MaskGUI, ScaleMode.StretchToFill, true, 10.0f);
                 }
         else
         {
                 // If the player doesn't have the mask, kill him.
                 AudioSource.PlayClipAtPoint(GasPoisoning, transform.position);
                 yield WaitForSeconds(2);
                 GameObject.Destroy(player);
                 Debug.Log ("Gas Poidoned");
             }
         }
         }
    
 function OnTriggerExit (other : Collider)
 {
         if(other.gameObject.name == "Player"){    
                 AudioSource.PlayClipAtPoint(GasmaskGrab, transform.position);
         }
 }
Answer by KiraSensei · Aug 27, 2014 at 10:41 PM
GUI methods work in OnGUI function, not OnTriggerEnter. Try this :
 private var gasMask:boolean = false;
 
 function OnTriggerEnter (other : Collider)
 {
     if(other.gameObject.name == "Player"){
         // ... if the player has the mask...
         if(playerInventory.hasMask){
             AudioSource.PlayClipAtPoint(GasmaskGrab, transform.position);
             gasMask = true;
         }
         else
         {
             // If the player doesn't have the mask, kill him.
             AudioSource.PlayClipAtPoint(GasPoisoning, transform.position);
             yield WaitForSeconds(2);
             GameObject.Destroy(player);
             Debug.Log ("Gas Poidoned");
         }
     }
 }
 
 function OnTriggerExit (other : Collider)
 {
     if(other.gameObject.name == "Player"){    
         AudioSource.PlayClipAtPoint(GasmaskGrab, transform.position);
         gasMask = false;
     }
 }
 
 function OnGUI() {
     if(gasMask) GUI.DrawTexture(Rect(10,10,60,60), MaskGUI, ScaleMode.StretchToFill, true, 10.0f);
 }
that's correct but I think you missed an if in the OnGUI part, like if(gas$$anonymous$$ask) GUI.DrawTexture(...)!
Well the script itself works. However the GUI appears as soon as I launch the game and stays all the time.
link7 is right, the correct code is :
 function OnGUI() {
     if(gas$$anonymous$$ask) GUI.DrawTexture(Rect(10,10,60,60), $$anonymous$$askGUI, Scale$$anonymous$$ode.StretchToFill, true, 10.0f);
 }
Ok. I tried this script. But with an old one at least I had the texture appearing. Now it kicks in this error: NullReferenceException: Object reference not set to an instance of an object GasfieldTrigger+$OnTriggerEnter$40+$.$$anonymous$$oveNext () (at Assets/GasfieldTrigger.js:14)
Your answer
 
 
              koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                