• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
5
Question by Sl0thzy · Oct 25, 2015 at 04:56 PM · uiguibuttonscript.

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)

Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image fafase · Oct 25, 2015 at 06:55 PM 0
Share

Try removing UI.Button from the declaration.

avatar image Sl0thzy fafase · Oct 25, 2015 at 09:18 PM 0
Share

Button.ClickedEvent isn't a class :/

avatar image Derpyness · Oct 25, 2015 at 11:11 PM 0
Share

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();

2 Replies

· Add your reply
  • Sort: 
avatar image
8

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;
         }
     }
 }
 
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ktduffyinc_unity · Jun 09, 2019 at 02:26 PM 0
Share

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

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

UI Button and Color Tint Transition issue 0 Answers

Button onClick events will not be triggered 1 Answer

Animation script help light problem 0 Answers

How to draw GUI texture under UI elements? 0 Answers

How to have one GUI button on top of another? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges