Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by 0_osmile0_o · Aug 27, 2014 at 10:24 PM · guitextureguitexture

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);
         }
 }
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image llnk7 · Aug 27, 2014 at 10:58 PM 0
Share

that's correct but I think you missed an if in the OnGUI part, like if(gas$$anonymous$$ask) GUI.DrawTexture(...)!

avatar image GraviterX · Aug 27, 2014 at 11:56 PM 0
Share

Change AudioSource in line 23 to Audio

avatar image 0_osmile0_o · Aug 28, 2014 at 08:02 AM 0
Share

Well the script itself works. However the GUI appears as soon as I launch the game and stays all the time.

avatar image KiraSensei · Aug 28, 2014 at 06:07 PM 0
Share

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);
 }
avatar image 0_osmile0_o · Aug 29, 2014 at 12:09 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

How to make a GUI Texture (image) transparent? 2 Answers

Texture's Alpha is not Transparent when I use it as a GUI.label 1 Answer

Show GUI texture on collison 1 Answer

How to script GUI Texture Window to see it in Script (Object's component) 1 Answer

Gui Texture Swap? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges