Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Hydroid · Dec 18, 2016 at 11:21 AM · buttontextfloatint

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.

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

4 Replies

· Add your reply
  • Sort: 
avatar image
0

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!

Comment
Add comment · Show 3 · 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 Hydroid · Dec 18, 2016 at 11:03 PM 0
Share

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.

avatar image Creeper_Math Hydroid · Dec 19, 2016 at 01:07 AM 0
Share

Ok I edited it, hope it works now!

avatar image Hydroid Creeper_Math · Dec 19, 2016 at 03:41 AM 0
Share

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' ‎

avatar image
0

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;
     }
 
 
 }
Comment
Add comment · Show 2 · 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 Hydroid · Dec 19, 2016 at 12:53 AM 0
Share

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'"

avatar image El-Deiablo Hydroid · Dec 19, 2016 at 08:34 AM 0
Share

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!

avatar image
0

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; 
  }
 }
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
avatar image
0

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;
 }
 
 
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

7 People are following this question.

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

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


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