- Home /
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...
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.)
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.
I got it! Thanks aidonaletto. It took a bit of trial and error - but I got there in the end.
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?
@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);