- Home /
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
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.
Thanks a lot, I will try this tomorrow when I start work again.
How to I reference the string that has just been created from the Float?
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
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?
Your answer
Follow this Question
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