- Home /
Question by
Side · Oct 10, 2012 at 09:35 PM ·
errorgameobjectgetcomponent
GameObject.getComponent problem
This code did sound quite plausible for me, but it failed miserably. I guess its easy to understand what I want to do... What am I doing wrong?
This is my code:
GameObject.FindWithTag("Tag_Here").getComponent("Component_Here").VariableHere = true;
And the error:
MissingMethodException: Method not found: 'UnityEngine.GameObject.getComponent'.
Comment
Best Answer
Answer by Bunny83 · Oct 10, 2012 at 11:29 PM
You have two problems in your script:
The function GetComponent is called GetComponent and not getComponent.
You shouldn't use the string version of GetComponent since the compiler can't determine the type it should return.
Do it like this in UnityScript:
GameObject.FindWithTag("Tag_Here").GetComponent(Component_Here).VariableHere = true;
and like this in C#:
GameObject.FindWithTag("Tag_Here").GetComponent<Component_Here>().VariableHere = true;
Answer by Sundar · Oct 10, 2012 at 09:47 PM
Debug first like this
GameObject go = GameObject.FindWithTag("Tag_Here");
if( go )
{
if(go.GetComponent("Component_Here") )
{
go.GetComponent("Component_Here").VariableHere = true;
}
else print("Component_Here not found");
}
else print("GameObject with Tag_Here not found");
You will know whatz wrong.