Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

[SOLVED] Toggle.isOn not compiling help

Discussion in 'Immediate Mode GUI (IMGUI)' started by No-jo, Oct 6, 2014.

  1. No-jo

    No-jo

    Joined:
    Aug 30, 2014
    Posts:
    8
    Hi,

    I'm using Unity 4.6.0b17 and for some reason isOn is always red in MonoDevelop and when I come to compile I get the following error:

    This is my Script:

    Code (CSharp):
    1. using UnityEngine;
    2. using UnityEngine.UI;
    3. using System.Collections;
    4.  
    5.  
    6. public class ToggleMusic : MonoBehaviour {
    7.  
    8.     void Start()
    9.     {
    10.         var toggle = GetComponent<Toggle>();
    11.      
    12.         if (GlobalVar.music == false)
    13.         {
    14.             toggle.isOn = true;
    15.         }          
    16.     }
    17.  
    18.     public void click()
    19.     {
    20.         GlobalVar.music = !GlobalVar.music;
    21.         Debug.Log(GlobalVar.music);
    22.         //bool result = gameObject.GetComponent<Toggle>().isOn;
    23.     }
    24.  
    25.  
    26. }
    27.  
    Can anyone tell me where i'm going wrong please I'm fairly new to Unity and would really appreciate any help?

    Thanks
     
    Last edited: Oct 6, 2014
  2. No-jo

    No-jo

    Joined:
    Aug 30, 2014
    Posts:
    8
    Ah got it:

    Code (CSharp):
    1. GetComponent<UnityEngine.UI.Toggle>();
    Instead of:

    Code (CSharp):
    1. GetComponent<Toggle>();
    So:

    Code (CSharp):
    1. void Start()
    2.     {
    3.         var toggle = GetComponent<UnityEngine.UI.Toggle>();
    4.      
    5.         if (GlobalVar.music == false)
    6.         {
    7.             toggle.isOn = true; //but this makes music true?!
    8.             GlobalVar.music = false; //then turn it back off then
    9.      
    10.         }        
    11.     }
     
    Last edited: Oct 6, 2014