- Home /
Display a float number when button is clicked
Hello!
I'm making a game and there is a button that I want to have a specific float variable appear for a couple of seconds on where the button was clicked and then slide up and disappear.
I don't know how to do this so any help would be great.
Thanks in advance.
Answer by Creeper_Math · Dec 18, 2016 at 02:50 PM
So first you will need to make a text object near or on the button. I assume you know about setting variables with the public types and such. Here's a sample script that might work
using UnityEngine.UI; // Add this line to the top where all the other "using" commands are
//------------------------
public float FloatToDisplay;
public Text FloatDisplayText;
public GameObject ObjectToLift;
public RectTransform ObjectLiftTransform;
public float TimeToDisplayFor; // Time in seconds to display the float
public Vector3 MovementVariable; // How far it should go in units per second
bool DisplayingText;
//This Is for detecting if it's off the screen
public Camera DisplayCamera;
public float ExtraTime; // How long should the object wait before destroying the script after it's off the screen
public void LiftText() { // Custom Fuction to be called by the button
FloatToDisplay.Text = FloatToDisplay.ToString();
DisplayingText = true;
}
void Update() {
if (DisplayingText == true) {
TimeToDisplayFor += -1 * Time.deltaTime;
}
if (TimeToDisplayFor < 0) {
ObjectToLift.transform.position += MovementVariable * Time.DeltaTime;
}
if (ObjectLiftTransform.transform.position.y > DisplayingCamera.ScreenToWorldPoint(new Vector3(screen.width,screen.height,0).y) {
Destroy(this,ExtraTime);
}
}
You will now need to go into the button, and add a function in the "on click" input field. You then should locate the object that this script is on and select the "Lift Text" custom function. Hopefully that works!
Okay so I did as told but get these four errors.
'Text' is a type but it is used like a variable.
'GameObject' does not contain a definition for 'recttransofrm' and no extension method 'recttransofrm' accepting a first argument of type 'GameObject' could be found.
(x2) 'float' does not contain a definition for 'DeltaTime' and no extension method 'DeltaTime' accpeting a first argument of type 'float' could be found.
Lol I now have 5 errors.
'float' does not contain a definition for 'Text' and no extension method 'Text' accepting a first argument of type 'float' could be found (are you missing a using directive or an assembly reference?)
cannot convert from 'float' to 'UnityEngine.Vector3'
The name DisplayingCamera' does not exist in the current context Solution 'Email Clicker' Type
float' does not contain a definition for Text' and no extension method
Text' of type float' could be found (are you missing a using directive or an assembly reference?) and 'UnityEngine.Time' does not contain a definition for
DeltaTime' Solution 'Email Clicker'
Answer by El-Deiablo · Dec 18, 2016 at 04:19 PM
You could try something like this:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Test : MonoBehaviour {
public Text text;
private float duration;
private RectTransform rectTransform; //so you can anchor the position
private Vector2 textStartPosition, textEndPosition;
private bool spawned;
float spawnLength; //enter a float for the amount of time text stays visible
void Start() {
rectTransform = text.GetComponent<RectTransform>();
textStartPosition = rectTransform.anchoredPosition;
textEndPosition = new Vector2(0,200);//if your button was at the bottom and you wanted to move up
duration = 4f;
spawnLength = 3f;
}
void Update() {
if (spawned == true){
StartCoroutine(TravelText());
}
else if(spawned == false){
StopCoroutine(TravelText());
}
}
//you would attach the below function to your button or just add the bool to the function you've already created if there's more going on
public void Button(){
spawned = true;
}
IEnumerator TravelText() {
text.enabled = true;
float elapsedTime = 0;
while (elapsedTime < duration) {
float t = elapsedTime / duration;
rectTransform.anchoredPosition = Vector2.Lerp(textStartPosition,textEndPosition,t);
elapsedTime += Time.deltaTime;
yield return new WaitForSeconds(spawnLength);
}
text.enabled = false;
spawned = false;
}
}
Okay so I got 3 errors but those were easy to fix. Just a spelling error for the first two. But the last one, i can't figure out. It says; "argument 2: cannot convert from 'UnityEngine.Vector2' to 'UnityEngine.Transform'"
I edited the code and tested. With this code you may include and alter other variables of the text such as the color (including alpha for fading). Hope this helps!
Answer by SolAZDev · Dec 19, 2016 at 08:29 AM
C# and possibly not the best but~
public float DispFloat;
public TextMesh tMesh;
void OnMouseOver(){
if(Input.GetMouseButtonDown(0)){
tMesh.text = DispFloat.ToString();http://answers.unity3d.com/questions/1286787/display-a-float-number-when-button-is-clicked.html#
}else{
tMesh.text = System.String.Empty;
}
}
void OnMouseExit(){
if(tMesh.text != System.String.Empty){
tMesh.text = System.String.Empty;
}
}
Answer by SolAZDev · Dec 19, 2016 at 08:29 AM
C# and possibly not the best but.
public float DispFloat=0f;
public TextMesh tMesh;
void OnMouseOver(){
if(Input.GetMouseButton(0)){
tMesh.text = DispFloat.ToString();
}else{
tMesh.text = System.String.Empty;
}
}
void OnMouseExit(){
tMesh.text = System.String.Empty;
}
Your answer
Follow this Question
Related Questions
Have a problems with a values 1 Answer
Number 0 not showing up in GUI.Button 2 Answers
Accessing text component. 2 Answers
Convert a char to int / float 2 Answers
What the hell? Cannot convert float to int PROBLEM. 2 Answers