- Home /
This post has been wikified, any user with enough reputation can edit it.
Question by
CrilleStyles · Aug 08, 2014 at 09:19 AM ·
getcomponentif-statements
Create a if statement using GetComponent?
I'm wondering if you can use an if statement in a Getcomponent. I think it's something like this But, How do you make if statements using getcomponent?
var Bob : GameObject; //The player's name is bob in this example
function Update () {
if (Bob.GetComponent(myExample).MyValue = 0){
DoSomething();
}
Comment
Best Answer
Answer by darthtelle · Aug 08, 2014 at 09:22 AM
Say in your class myExample you have a variable called MyValue. You can call GetComponent to access those values. You're close in your solution!
if(Bob.GetComponent<myExample>().MyValue == 0)
{
DoSomething();
}
However, keep in mind if you are using GetComponent in an Update loop it is very costly! Usually it's best to store a reference to it!
Your answer
![](https://koobas.hobune.stream/wayback/20220613160127im_/https://answers.unity.com/themes/thub/images/avi.jpg)