- Home /
Unity Gui lable problem with showing js var using C#
i get the following errors on my script: Assets/Scripts/HUD.cs(27,21): error CS1502: The best overloaded method match for UnityEngine.GUI.Label(UnityEngine.Rect, string)' has some invalid arguments AND Assets/Scripts/HUD.cs(27,21): error CS1503: Argument
#2' cannot convert float' expression to type
string'
My script HUD.cs contains the following code:
using UnityEngine;
using System.Collections;
public class HUD : MonoBehaviour
{
//create a variable to access the JavaScript script
private Health jsScript;
void Awake()
{
//Get the JavaScript component
jsScript = this.GetComponent<Health>();//Don't forget to place the 'JS1' file inside the 'Standard Assets' folder
}
//render text and other GUI elements to the screen
void OnGUI()
{
//render the JS1 'message' variable
GUI.Label(new Rect(10,10,300,20), jsScript.health);
}
}
and my JS code is the following:
var MaxHealth=100.0;
var health = MaxHealth;
var regenTime = 20.0;
var regenSpeed = 3.0;
private var timer = 0.0;
function Update(){
if(health <= 0){
die();
}
if(health>MaxHealth){
health=MaxHealth;
}
if(health < MaxHealth){
if(timer < regenTime){
timer+=0.1;
}else{
health+=regenSpeed/10;
}
}else{
timer = 0.0;
}
}
function die(){
}
Answer by zharik86 · Dec 29, 2013 at 10:52 AM
Yes, such problem takes place to be. Second parameter in fact it not string, and GuiContent. That your code earned, change it to the following:
GUI.Label(new Rect(10,10,300,20), "" + jsScript.health);
On CSharp it helps. It is possible to try and other option:
GUI.Label(new Rect(10,10,300,20), new GUIContent(jsScript.health));
It seems, it shall help, but precisely isn't sure (I write everything on CSharp therefore sometimes there are difficulties with the translation into other programming language). And just in case, whether it is necessary to check there is an opportunity to convert float value in string as:
jsScript.health.ToString();
Thank you man, this was really valuable information to me considering i will be using it often :D!
Answer by Graham-Dunnett · Dec 29, 2013 at 10:36 AM
Your variable health
hasn't been typed. You are using it like a number, but GUI.Label
expects a string.
Your answer
Follow this Question
Related Questions
Networkmanager errors and gui to small 0 Answers
Error in my script i don't understand. 0 Answers
How do I change the text of a gui image text 1 Answer
Can I use the new GUI system with instantiated prefabs? 1 Answer
script error help 0 Answers