Home > Net >  Trying to get a variable form a script in public gameobject, error CS1061
Trying to get a variable form a script in public gameobject, error CS1061

Time:05-30

I am trying to get a variable "isOffRoad" from script "WheelHandler" in the object "Wheel1". I am getting the error:

Assets\Scripts\CarControler.cs(53,13): error CS1061: 'GameObject' does not contain a definition for 'WheelHandler' and no accessible extension method 'WheelHandler' accepting a first argument of type 'GameObject' could be found (are you missing a using directive or an assembly reference?)

Thanks in advance.

Edit: isOffRoad is public.

public GameObject Wheel1;

void Update()
    {
        if(Wheel1.WheelHandler.isOffRoad == true)
        {
            Debug.Log("offroad");
        }
    }

CodePudding user response:

You need to access your component with GetComponent<>() method.

In your case that would be Wheel1.GetComponent<WheelHandler>().isOffRoad

Also It is not recommended to access component in Update loop. It would be better to cache it in a field.

CodePudding user response:

Try this one:

if (Wheel1.GetComponent<WheelHandler>().isOffRoad))
{
    // do something..
}
  • Related