Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
0
Question by MightyMatty · Nov 05, 2011 at 12:36 AM · guiinputcountergetbuttondown

Updating GUI text with GetButtonDown?

Hi

Firstly, apologies if the answer to this question already exists, following 2 hours of searching I am drawing a blank - and its likely down to minimal coding experience and not spotting the gem of a script that'd do the job. I really do appreciate any help and advice of any kind.

So, what I am trying to achieve is an update to some GUI text each time the player clicks the left mouse button. My game involves volleying a soccer ball - and I essentially want this displayed as a score via GUI text.

I have a GUI Text GameObject - which is numerical and starts at 0. Each time I click (the input for keeping the ball up) i'd like this text to increment and update. Here is what I am trying to do...and its most likely I am trying to reinvent the wheel and completely failing in my logic:

 var guiText : GUIText;
     
 var clickCount : float= 0;
     
     function Update() {
     
     if(Input.GetButtonDown("Fire1"))
     
         {
     
             guiText.text = clickCount++;
     
         }
     
     }


 

The error I currently get after assigning the script to the Gui Text (value) is:

Assets/GuiCounter.js(7,17): BCE0004: Ambiguous reference 'guiText': GuiCounter.guiText, UnityEngine.Component.guiText.

Any advice would be awesome. I see where the error is, just haven't got a clue where to go with it. My simple mind tells me this should work but its obviously completely wrong. Please don't laugh too hard...

Comment
Add comment
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by aldonaletto · Nov 05, 2011 at 12:52 AM

guiText is the name of a property - Component.guiText, as the message error inform - but you created a variable with this same name. If this script is attached to the GUIText object, just remove the variable declaration at the beginning and you will use the property directly. If it's attached to other object, change the variable name to anything but guiText (GuiText, gText, myGuiText etc.)

Comment
Add comment · Show 4 · 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 MightyMatty · Nov 06, 2011 at 10:44 AM 0
Share

Thanks for the advice. I've been at it for a day now and no joy. I'm afraid I'm still struggling to understand being a complete n00b, its 4:16 am and i'm going to my bed :O I refuse to script hunt. The sense of achievement when it starts to work will be awesome.

avatar image MightyMatty · Nov 08, 2011 at 11:08 PM 0
Share

I got it! Thanks aidonaletto. It took a bit of trial and error - but I got there in the end.

avatar image elrobbo6 · Nov 22, 2011 at 09:52 PM 0
Share

Ok, so i am trying this except i am getting a "Cannot convert 'float' to 'string'" error. Any suggestions on why or how to fix it?

avatar image Bunny83 · Nov 22, 2011 at 10:00 PM 0
Share

@elrobbo6: If you have a question, why do you post an answer?.... I've converted your answer into a comment.

Well the problem is that GUIText.text is a string so you have to assign a string to it. clickCount is a float number so you have to convert it into a string. Either by using .ToString() or by implicitly converting it to a string. Two examples:

 guiText.text = (clickCount++).ToString();
 
 // or implicitly
 guiText.text = "" + (clickCount++);


Don't forget that the ++opperator he uses is the post-increment-opperator so it would use the current number for the display and after that it will increment the number. Usually the pre-increment-opperator makes more sense:

 guiText.text = "" + (++clickCount);

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Shot Counter Question 2 Answers

gui list problem 2 Answers

GUI texture touch input problem 1 Answer

How do I handle multitouch as Input.GetButtonDown()? 0 Answers

How do I get a GUI Texture Button to act as a Input Key such as a T button on a key bored or so? 1 Answer


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