'Unity button interactable toggle
so I want to make a script, that when the image of a sprite is set to something, enable a button, but if it isn't, disable it. This script is my latest attempt, and it only has 1 error which is "The type or namespace name "Button" could not be found (are you missing a using directive or an assembly reference?)". Also my script is put directly on the buttons just to make it easier for myself. Here is my script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AgreeDisagreeManager : MonoBehaviour
{
SpriteRenderer m_SpriteRenderer;
void Start()
{
m_SpriteRenderer = GameObject.Find("Square").GetComponent<SpriteRenderer>();
}
public Sprite Menu,MenuTerms;
Button ButtonCompo;
void Update()
{
if(m_SpriteRenderer.sprite == Menu || m_SpriteRenderer.sprite == MenuTerms)
{
ButtonCompo = GameObject.GetComponent<Button>();
ButtonCompo.interactable = false;
}
else
{
ButtonCompo = GameObject.GetComponent<Button>();
ButtonCompo.interactable = true;
}
}
}
This is like my 3rd day ever using unity so if the code is bad, thats why
Solution 1:[1]
using UnityEngine.UI;
Button comes from UnityEngine.UI namespace.
These sorts of simple issues are notified by an IDE (ie. Visual Studio), which you can hover like so:
Clicking on them gives you some solution:
Though not perfect, but helps a lot with the issues of finding the relevant namespaces or typos.
Specially when you are starting out in Unity and copying code-sources from somewhere, as they may reference to some obscure Unity namespace.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Kaynnc |


