0
\$\begingroup\$

I have a simple class attached to a button in Unity Canvas:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UIElements;

public class AudioButtonPlay : MonoBehaviour
{
    public Button button;
    void Start()
    {
        button =  gameObject.GetComponent<Button>();
    }
}

But on the line button = gameObject.GetComponent<Button>(); I get the following message:

GetComponent requires that the requested component 'Button' derives from MonoBehaviour or Component or is an interface

How can I access the "Button" component?

\$\endgroup\$
2
\$\begingroup\$

You're using the wrong namespace.

Instead of

using UnityEngine.UIElements;

do

using UnityEngine.UI;

Everything else should already work when you change that.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.