BCE0023: No appropriate version of 'UnityEngine.Component.GetComponent' for the argument list '(UnityEngine.TextMesh)' was found.
New to programming so sorry if this is simple. I am trying to use a textmesh code to edit a 3D text in game. basically what i have done is assigned an invisible integer as its health, that integer is sent to a string so i can use that string to edit the 3D text (hopefully that makes sense) Here is my code.
#pragma strict
var Militia : GameObject;
var hp : GameObject;
var cube : GameObject;
var trigger : GameObject;
var red_hp : int = 100;
var red_range : boolean = false;
var hp_value : String ="";
var hp_real : TextMesh;
function Start ()
{
Militia.SetActive(false);
hp.SetActive(false);
red_range = false;
}
function Update ()
{
if(Input.GetKeyUp(KeyCode.F))
{
if(red_range == true)
{
red_hp = red_hp - 25;
hp_value= red_hp.ToString();
GetComponent(hp_real).text = hp_value; << Error is here
if (red_hp <=0)
{
Destroy (cube);
Destroy (Militia);
Destroy(hp);
}
}
}
}
function OnTriggerEnter (trigger : Collider)
{
Militia.SetActive(true);
hp.SetActive(true);
red_range = true;
}
function OnTriggerExit (trigger : Collider)
{
if (red_hp >=0)
{
Militia.SetActive(false);
hp.SetActive(false);
red_range = false;
}
}
Any help will be appreciated
Thanks
Answer by ElijahShadbolt · Nov 15, 2016 at 07:10 AM
hp_real = GetComponent<TextMesh>();
hp_real.text = hp_value;
Edit: oh, right, it's in JS, not C#.
hp_real = GetComponent.<TextMesh>();
hp_real.text = hp_value;
replaced my
GetComponent(hp_real).text = hp_value; << Error is here
With your
hp_real = GetComponent<Text$$anonymous$$esh>();
hp_real.text = hp_value;
But i have 3 new errors now:
Assets/test.js(25,46): BCE0043: Unexpected token: ).
Assets/test.js(25,47): BCE0044: expecting ), found ';'.
Assets/test.js(25,48): UCE0001: ';' expected. Insert a semicolon at the end.
I have copied yours exactly so im not sure why its telling me to add brackets and semicolons.
Answer by Moritz13 · Nov 15, 2016 at 07:22 AM
Never mind fixed it. Took out hp_real = GetComponent(); And it worked.
Thank you for your help