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
1
Question by OoglyWoogly · Apr 13, 2011 at 05:51 PM · javascriptstringfloatconversion

How to convert a float to a string and then have it appear in a gui rect

Hi all, Just a quick syntax question as the Unity documentation was pretty much useless in this regard. I am trying to fetch a variable from one script (float) and put it in another (as a string) that is constantly updating. I have perused the forums but found nothing of use in this regard. Here is my javascript so far:

function Update () 
{
getHorSpeed = gameObject.GetComponent(PlayerMovement.horAcceleration).ToString();
}
function OnGUI () 
{
    GUI.Box (Rect (Screen.width - 100, Screen.height - 50, 100, 50), GUIContent ("Speed", getHorSpeed));
}

At the moment the syntax is wrong for the GetComponent line. Can someone help me with this or point me at some good reference documentation? 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

3 Replies

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

Answer by Kourosh · Apr 13, 2011 at 05:57 PM

here is the fix: "I assume PlayerMovement is your script"

getHorSpeed = gameObject.GetComponent("PlayerMovement").horAcceleration.ToString();

The horAcceleration should be defined static to be able to access it globally.

Hope it helps.

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 OoglyWoogly · Apr 13, 2011 at 07:16 PM 0
Share

Thanks a lot, I will try this tomorrow when I start work again.

avatar image OoglyWoogly · Apr 13, 2011 at 07:43 PM 0
Share

How to I reference the string that has just been created from the Float?

avatar image
2

Answer by unaimed · Sep 12, 2011 at 10:38 AM

using UnityEngine;

using System.Collections;

public class NAMEOFSCRIPT : MonoBehaviour {

 public string horSpeed;
 public PlayerMovement pm;
 
 void Start () {
     
     
     horSpeed = pm.horAcceleration.ToString();
 
 
 }
 
 void OnGUI(){
     
     GUI.Box (new Rect (Screen.width - 100, Screen.height - 50, 100, 50), horSpeed);    
 }
 }


this is c# but try it, attach the right script (if needed), change NAMEOFSCRIPT to the name of the c# script

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 OoglyWoogly · Apr 13, 2011 at 10:39 PM

I am now getting a NullReferenceException: Object reference not set to an instance of an object on the line starting with getHorSpeed var horSpeed : String;

function Update () { getHorSpeed = gameObject.GetComponent("PlayerMovement").horAcceleration.ToString(); var horSpeed: String = getHorSpeed; } function OnGUI () { GUI.Box (Rect (Screen.width - 100, Screen.height - 50, 100, 50), GUIContent (horSpeed));

}

How do I reference the product of the ToString method?

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

1 Person is following this question.

avatar image

Related Questions

Convert Array into One String (Js) 1 Answer

how to convert string into numbers in JS 1 Answer

3d text will not display float 1 Answer

Converting a .CSV string array to a float array 1 Answer

To String with 'n' zeros in front 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