- Home /
The Type Or Namespace UI could not be found - Trying to create a clickable button
Hello! So I am currently trying to create a little weapon shop for a personal project I am working on and I am having a lot of trouble that 45 min of Google could not solve. The biggest problem so far is that Unity is not registering the 'UI' class and I can not figure out why. Here is the current script that I have:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Events;
public class Master : MonoBehaviour {
public Button store;
public UI.Button.ButtonClickedEvent onClick;
public Transform hands;
public GameObject gun1;
// Use this for initialization
void Start () {
store = GetComponent<Button>();
gun1 = GameObject.FindGameObjectWithTag("Weapon1");
hands = GameObject.FindGameObjectWithTag("Player").transform;
onClick = Instantiate(gun1, hands.position, hands.rotation);
}
// Update is called once per frame
void Update () {
store.onClick;
}
}
The exact errors I get are: Severity Code Description Project File Line Error CS0246 The type or namespace name 'UI' could not be found (are you missing a using directive or an assembly reference?) TDS-AYYYY.CSharp C:\Users\Gavin\Documents\TDS-AYYYY\Assets\Scripts\Master.cs 9
(Line 9 is public UI.Button.ButtonClickedEvent onClick;)
And
Severity Code Description Project File Line Error CS0201 Only assignment, call, increment, decrement, and new object expressions can be used as a statement TDS-AYYYY.CSharp C:\Users\Gavin\Documents\TDS-AYYYY\Assets\Scripts\Master.cs 23
(Line 23 is store.onClick)
Did you remember to set the Button variable in the inspector. If you did and it didn't work, try using
store = GameObject.Find(ButtonNameInEditor).GetComponent();
Answer by Krazeecain · Apr 21, 2016 at 09:14 PM
I had the same problem and stumbled on this question via google. I fixed it by adding "using UnityEngine.UI" at the top as you did, and then simply removing "UI." from my declarations. It's strange... it must be a bug with Unity itself because there's no reason that using UI.Button as a declaration should fail, I've done it before (at least I have with UI.Text).
Here's my script, it works perfectly now after that tweak:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
[RequireComponent(typeof(Text))]
public class ResourceDisplay : MonoBehaviour {
enum resEnum {Wood, Iron, Stone, Gold, Food, Population }
resEnum resSelect;
private Text txtComp;
void Start () {
txtComp = GetComponent<Text>();
}
void Update () {
switch (resSelect)
{
case resEnum.Wood:
txtComp.text = GVars.qWood.ToString();
break;
case resEnum.Iron:
txtComp.text = GVars.qIron.ToString();
break;
case resEnum.Stone:
txtComp.text = GVars.qStone.ToString();
break;
case resEnum.Gold:
txtComp.text = GVars.qGold.ToString();
break;
case resEnum.Food:
txtComp.text = GVars.qFood.ToString();
break;
case resEnum.Population:
txtComp.text = GVars.qPop.ToString();
break;
}
}
}
Oh my goodness! @$$anonymous$$razeecain you just saved meeeee!!! TYTYTYT!
was having a similar but slightly different issue which I documented here https://answers.unity.com/questions/1638555/guitexture-adn-guitext-are-obsolete-standard-asset.html?childToView=1639151#answer-1639151
Answer by HumblPi · May 15 at 05:14 PM
Wow, this saved my butt too. Just transferred an old project to a new computer, and this was just headscratching. Thank you!
Your answer
Follow this Question
Related Questions
UI Button multiple parameters 6 Answers
Move gameobject to button in new 4.6? 0 Answers
Buttons not Clickable inside of ScrollView 0 Answers
Force Unity UI element to refresh/update? 4 Answers
Why isn't curved UI asset working? 0 Answers