Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Mr_Love_Gloves on July 09, 2014, 05:24:20 PM

Title: Object reference not set to an instance of an object (Toggles)
Post by: Mr_Love_Gloves on July 09, 2014, 05:24:20 PM
0
I cant deal with these errors >:[ any help on this one??

Error: NullReferenceException: Object reference not set to an instance of an object SoundButtionGUI.Update () (at Assets/SoundButtionGUI.cs:27)

Im getting the errors with : if(toggle.value == true)
>> this is the variable: public UIToggle toggle;

  1. if (Application.loadedLevel == 0) {
  2.             if(toggle.value == true){
  3.                 PlayerPrefs.SetInt("SoundMute", 1);
  4.                 Invoke("audioOff", 0.01f);
  5.             } else{
  6.                 Invoke("audioOn", 0.01f);
  7.                 PlayerPrefs.SetInt("SoundMute", 0);
  8.             }
  9.         }

Ive tried this:
if(toggle.value == true && toggle != null && toggle.value != null && toggle.value != false)
Also: if(toggle == true) which stops the error but the code does not work because toggle sometimes returns null
Any help would be great


Any help would be great, I have had loads of these errors and have had to ask on here for every single one -_-
Title: Re: Object reference not set to an instance of an object (Toggles)
Post by: ArenMook on July 09, 2014, 05:42:32 PM
Quote
Im getting the errors with : if(toggle.value == true)
Obvious reason? 'toggle' is null.
Title: Re: Object reference not set to an instance of an object (Toggles)
Post by: Mr_Love_Gloves on July 09, 2014, 06:01:40 PM
Thanks for the quick reply, I found that out I just dont know what would be a fix to that? any ideas? :P
Title: Re: Object reference not set to an instance of an object (Toggles)
Post by: Mr_Love_Gloves on July 09, 2014, 06:07:18 PM
if(toggle != null && toggle.value == true )
I changed it to that which did stop the error, but now when i go through the levels and get back to the start screen the toggle button does not work as toggle is returning null
Title: Re: Object reference not set to an instance of an object (Toggles)
Post by: Mr_Love_Gloves on July 09, 2014, 06:08:35 PM
This is the code
  1.         public Texture2D icon;
  2.         public Texture2D iconClicked;
  3.         public GUISkin toggleSkin;
  4.        
  5.         private bool muteToggle = false;
  6.         private bool toggleImg = false;
  7.         public UIToggle toggle;
  8.        
  9.         // Use this for initialization
  10.         void Awake () {
  11.                 if (PlayerPrefs.GetInt ("SoundMute") == 1) {
  12.                         toggle.value = true;
  13.                 } else {
  14.                         toggle.value = false;
  15.                 }
  16.         }
  17.         // Update is called once per frame
  18.         void Update () {
  19.                 if (Application.loadedLevel == 0) {
  20.  
  21.                                 if(toggle != null && toggle.value == true ){
  22.                                 PlayerPrefs.SetInt("SoundMute", 1);
  23.                                 Invoke("audioOff", 0.01f);
  24.                         } else{
  25.                                 Invoke("audioOn", 0.01f);
  26.                                 PlayerPrefs.SetInt("SoundMute", 0);
  27.                         }
  28.                 }
  29.         }
  30.  
  31.         void audioOn(){
  32.                 audio.volume = 1;
  33.         }
  34.        
  35.         void audioOff(){
  36.                 audio.volume = 0;
  37.         }
Title: Re: Object reference not set to an instance of an object (Toggles)
Post by: ArenMook on July 10, 2014, 08:06:24 PM
Never do anything in Awake() that involves other components. Do it in Start() instead. Awake() is like a constructor -- it should be used only for local variable initialization.