Search Unity

How to access variable of a prefab button?

Discussion in 'Scripting' started by Tiebo, May 1, 2017.

  1. Tiebo

    Tiebo

    Joined:
    Apr 26, 2017
    Posts:
    10
    Hi guys,

    as I didn't find a solution for this problem, I'm gonna ask here.

    I placed a toggle button from an asset prefab in my scene which contains a "Toggle (Script)" with the property/variable "Is On" giving the button's state.

    In a function I need to check this button's state. Normaly I use the GetComponent method as shwon in the code snippet but here that did not work, "Toggle" doesn't seem to be a valid Component

    Code (CSharp):
    1.  public GameObject myPrefab
    2. void Start (){
    3.      Debug.Log(myPrefab.GetComponent<Toggle>().IsOn);
    4. }
    Any idea what I'm doing wrong?

    Thanks in advance,

    Tobi
     
  2. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,775
    Do you have "using UnityEngine.UI;" at the top of the script?
     
  3. Tiebo

    Tiebo

    Joined:
    Apr 26, 2017
    Posts:
    10
    That's it! Thank you !