Search Unity

Menu for change Quality in runtime: Button.onClick.AddListener?

Discussion in 'UGUI & TextMesh Pro' started by teresamadruga, Jan 15, 2015.

  1. teresamadruga

    teresamadruga

    Joined:
    Nov 2, 2012
    Posts:
    6
    Hi, I wanted to use the following code to change the game quality with a script that takes the available qualities and transforms them in a runtime dropdown menu

    It was wrote first in a unity project, whose only purpose is testing things, and it works.

    But, when placed in another project that has a game in, it produces on red line the error CS1061: Type `Button' does not contain a definition for `onClick' and no extension method `onClick' of type `Button' could be found (are you missing a using directive or an assembly reference?)

    I don`t understand why the same script gives error in one file and not in another, and it would be awesome if the UI script documentation had more examples

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    //using UnityEngine.Events;
    //using UnityEngine.EventSystems;

    public class QualityMenu : MonoBehaviour
    {
    string [] qualityNames;

    public GameObject buttonPrefab;
    public Transform qualityButtonsPanel;

    void Start ()
    {
    qualityNames = QualitySettings.names;

    CreateQualityMenu ();
    }

    void CreateQualityMenu ()
    {
    for (int i = 0; i < qualityNames.Length; i++)
    {
    GameObject button = Instantiate (buttonPrefab) as GameObject;
    button.GetComponentInChildren <Text> ().text = qualityNames ;
    int qualityIndex = i;
    button.GetComponent <Button> ().onClick.AddListener
    (
    () => {SetQuality (qualityIndex);}
    );
    button.transform.SetParent (qualityButtonsPanel, false);
    }
    }

    public void SetQuality (int index)
    {
    QualitySettings.SetQualityLevel(index, false);
    }
    }
     
  2. teresamadruga

    teresamadruga

    Joined:
    Nov 2, 2012
    Posts:
    6
    Solved: it must be some Button class that is overlapping with the Button in the project I am working on

    Found here: http://answers.unity3d.com/questions/866053/error-cs0426-the-nested-type-buttonclickedevent-do.html

    The new code would be:

    using UnityEngine;
    using System.Collections;
    using UnityEngine.UI;
    //using UnityEngine.Events;
    //using UnityEngine.EventSystems;

    public class QualityMenu : MonoBehaviour
    {
    string [] qualityNames;

    public GameObject buttonPrefab;
    public Transform qualityButtonsPanel;

    void Start ()
    {
    qualityNames = QualitySettings.names;

    CreateQualityMenu ();
    }

    void CreateQualityMenu ()
    {
    for (int i = 0; i < qualityNames.Length; i++)
    {
    GameObject button = Instantiate (buttonPrefab) as GameObject;
    button.GetComponentInChildren <Text> ().text = qualityNames ;
    int qualityIndex = i;
    UnityEngine.UI.Button btn = button.GetComponent <UnityEngine.UI.Button> ();
    btn.onClick.AddListener

    (
    () => {SetQuality (qualityIndex);}
    );

    button.transform.SetParent (qualityButtonsPanel, false);
    }
    }

    public void SetQuality (int index)
    {
    QualitySettings.SetQualityLevel(index, false);
    }
    }
     
    Last edited: Jan 20, 2015
    Feaver1968 likes this.
  3. Acid-NN-9

    Acid-NN-9

    Joined:
    Apr 29, 2015
    Posts:
    10
    Thank you!