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 verschurengiovanni · Aug 07, 2020 at 02:44 PM · scripting problemscript.scripting beginner

Show speed in UI

Hello,

I have a question, I want to show the speed of my script as a text value in the top left of the screen. From 0% to 100%.

My script is this using UnityEngine; using System.Collections;

 [System.Serializable]
 public partial class molen : MonoBehaviour
 {
     public float molensnelheid;
     public Rigidbody arm;
     public AudioSource Audio;
     public bool motorpower;
 
 
 
 
 
     public virtual void Update()
     {
 
         GetComponent<AudioSource>().pitch = molensnelheid / 70;
 
 
         this.GetComponent<HingeJoint>().useMotor = true;
         if (Input.GetKey("q"))
         {
             this.molensnelheid = this.molensnelheid + (-1 * Time.maximumDeltaTime);
         }
         if (Input.GetKey("a"))
         {
             if (this.molensnelheid < 0)
             {
                 this.molensnelheid = this.molensnelheid + (1 * Time.maximumDeltaTime);
             }
             if (this.molensnelheid > 0)
             {
                 this.molensnelheid = this.molensnelheid - (1 * Time.maximumDeltaTime);
             }
             if ((this.molensnelheid > -1.2f) && (this.molensnelheid < 1.2f))
             {
                 this.molensnelheid = 0;
             }
         }
 
         {
             int _7 = 2000;
             JointMotor _8 = this.GetComponent<HingeJoint>().motor;
             _8.force = _7;
             this.GetComponent<HingeJoint>().motor = _8;
         }
 
         {
             float _9 = this.molensnelheid;
             JointMotor _10 = this.GetComponent<HingeJoint>().motor;
             _10.targetVelocity = _9;
             this.GetComponent<HingeJoint>().motor = _10;
         }
 
         {
             bool _11 = false;
             JointMotor _12 = this.GetComponent<HingeJoint>().motor;
             _12.freeSpin = _11;
             this.GetComponent<HingeJoint>().motor = _12;
         }
         this.molensnelheid = Mathf.Clamp(this.molensnelheid, -100, 100);
     }
 
     public virtual void Start()
     {
         this.GetComponent<HingeJoint>().anchor = new Vector3(0, 1, 0);
         this.GetComponent<HingeJoint>().axis = new Vector3(0, 1, 0);
     }
 
 }

I want to show the "public float molensnelheid;" as a value on the screen. But i'm not sure on how to that. I hope someone can help me with this :)

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
1

Answer by FlorentAlbyon · Aug 07, 2020 at 02:55 PM

Not sure what molenselheid is but if you're juste looking to display a text the simplest solution is:

  • create a Text game object (right click in hierarchy, UI > Text).

  • Create a serializable field in your "molen" monobehavior. Just add:

     public Text myText;
    

(You will need to add "using UnityEngine.UI" at the top of your file)

  • then you just need to change the text value of your Text ui component in the update

     myText.text = molensnelheid.ToString();
    

Note: You don't have to use "this" everytime :)

Hope this helps

Comment
Add comment · Show 12 · 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 verschurengiovanni · Aug 07, 2020 at 03:03 PM 0
Share

That totally works. But how can I add a percentage? " int _7 = 2000;" "2000" is the maximum.

Thanks in advance :)

avatar image FlorentAlbyon verschurengiovanni · Aug 07, 2020 at 03:12 PM 0
Share

To add a percentage, you'll just have to compute it yourself, then display it in the same way. something like:

   float percentage = (yourValue * 100f)/maxValue;
avatar image FlorentAlbyon FlorentAlbyon · Aug 07, 2020 at 03:14 PM 0
Share

Also if you want to display a "%" sign in the text, you can juste concatenate strings together like so:

  myText.text = percentage.ToString() + "%";
Show more comments

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

259 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Point Counter Works Only Once! 1 Answer

How to double spirte/gameobject/prefab and control the result on those items? 0 Answers

Script executing through walls 1 Answer

Slowly increase joint limit over time 1 Answer

How to share varibles betweent scripts 4 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