In my game I have a line of code that is getting my PlayerController script, and setting it to a private variable, playerhealth. Next it should set it to a text element, but it doesn’t. This is the error I get:

This is the code on the line it says:
playerHealth = gameObject.GetComponent().PlayerHealth;
I also get another error saying that it cannot convert float to string (to set the text), I also do not know how to resolve this.
I have no idea on what this means and i have no idea what to do. I have tried to research this but got nothing. Here is a link to my whole code:
https://gist.github.com/avanti534/88d0d956e014b9fbb4d4
Here is a image that may help:
Hi,
this may help:
//on your player game object a script called PlayerController with:
var health : float;
//on your other text element
var player : GameObject; //assign this in the inspector
function Update()
{
GetComponent(UI.Text).text = player.GetComponent(PlayerController).helath.ToString();
}
The ToString() should solve your convert float to string…
I would recommend you looked at some tutorial out there - if you are planning to use JavaScript - I would strongly recommend the Walker Boys one.
many many videos but very well done and you will learn a lot
good luck,
I may have misunderstood your question, but this should work:
PlayerController controller = gameObject.GetComponent<PlayerController>();
if(controller!=null)
{
playerHealth = controller.PlayerHealth;
HealthText.text = playerHealth.ToString();
}
else
{
Debug.Error("No PlayerController component found on gameObject");
}