1

I want to access the HorizontalAxis variable from the CarAgent component (of the Taxi gameobject). It works fine when I try to access it from another gameobject, but when I try to access it in CarUserControl, which is also a Taxi component, it says that CarAgent doesn't exist.

This is the other gameobject's script and it works fine:

private float HorizontalAxis;

public void Start() {

     HorizontalAxis = GameObject.Find("Taxi").GetComponent<CarAgent>().HorizontalAxis;
}

// Update is called once per frame
public void Update()
{

    transform.rotation = new Quaternion(0, 0, HorizontalAxis, 360);

}

and this is the CarUserControl script:

 private void Start()
    {
        HorizontalAxis = GameObject.Find("Taxi").GetComponent<CarAgent>().HorizontalAxis;
}

How can I access the HorizontalAxis variable in CarUserControl ?

EDIT: I tried to access other classes in this script and it doesn't work neither. I got this script from the UnityStandardAssets/Vehicules/Car, so at the beginning, it is written:

namespace UnityStandardAssets.Vehicles.Car
{
[RequireComponent(typeof (CarController))]
public class CarUserControl : MonoBehaviour
{

I am new to unity and c# so does it change something. And if yes, how can I fix it?

Vincent Quirion
  • 389
  • 2
  • 4
  • 16
  • The first works because you're getting the object before getting the component: `GameObject.Find("Taxi")`. The second code snippet tries to get CarAgent from the same GameObject, without doing `GameObject.Find("Taxi")` first. – Fredrik Schön Dec 29 '18 at 19:49
  • @Maakep exactly ... but that is supposed to work .. that's why the OP asks here... – derHugo Dec 29 '18 at 23:05
  • Are you 100% sure both components are on the same root level GameObject and not somehow nested? – derHugo Dec 29 '18 at 23:06
  • @derHugo Yeah but the only answer is that OP is, like you're writing here^, trying to GetComponent on a GameObject that doesn't have the GameObject. It could be that OP has accidently added this script on another GameObject while thinking it's the correct one malfunctioning. I suggest searching for the class name in the hierarchy, which will highlight all objects with the script attached. Or, if added dynamically, add a `Debug.Log(gameObject.name);` in the Start() of CarUserControl to verify which objects are using it. – Fredrik Schön Dec 30 '18 at 00:06

3 Answers3

1

Finally, the problem was that the CarUserControl was in the standard assets folder and that the CarAgent script was not in that folder. Apparently, the standard assets folder does not compilate at the same time as other folders. See more here!

Vincent Quirion
  • 389
  • 2
  • 4
  • 16
0

Its likely that you are too low or too high in the gameobject chain.

Try

car = GetComponent<CarAgent>(this.parent);

OR

car = GetComponent<CarAgent>(this.child);
h = car.HorizontalAxis;
Olivier Jacot-Descombes
  • 104,806
  • 13
  • 138
  • 188
Kelso Sharp
  • 972
  • 8
  • 12
  • Can you explain to me what is in the parentheses please? – Vincent Quirion Dec 29 '18 at 20:26
  • "this" refers to the current gameobject, in your case its the CarUserControl – Kelso Sharp Dec 29 '18 at 20:35
  • Yes but what is it doing when you put it in the parentheses? – Vincent Quirion Dec 29 '18 at 20:36
  • You are passing the current gameobject parent or child into the GetComponent Method. – Kelso Sharp Dec 29 '18 at 20:38
  • it returns Type `UnityStandardAssets.Vehicles.Car.CarUserControl' does not contain a definition for `parent' and no extension method `parent' of type `UnityStandardAssets.Vehicles.Car.CarUserControl' could be found. Are you missing an assembly reference? – Vincent Quirion Dec 29 '18 at 20:45
  • I need to see more code to be able to say. please provide more information – Kelso Sharp Dec 29 '18 at 21:54
  • @KelsoSharp can you show me where you found a typed `GetComponent()` implementation that takes a parameter? Also note that there is no `this.parent` .. you probably mean `this.transform.parent` which is of type `Transform` but yet there is no implementation taking that as parameter – derHugo Dec 29 '18 at 23:09
  • Just a quick correction here, to avoid potential confusion; `this` is actually the MonoBehaviour component; the class you're writing it in. `this.gameObject` (or, for short, `gameObject`) is the gameObject. – Fredrik Schön Dec 30 '18 at 00:02
  • You probably mean `GetComponentInChildren(true);` or `GetComponentInParent();` – derHugo Dec 30 '18 at 12:24
  • I edited the question, so could you take a look please? – Vincent Quirion Jan 04 '19 at 02:46
0

It's mainly because there are inconsistencies between the Update() (every frame) and the FixedUpdate() (every physics frame). Indeed a FixedUpdate can be called more than once for the same Update() frame, or not at all.

Unity's doc about Order of Execution explains it more.

Also, querying GetComponent<T> in loops is quite heavy. It is wiser to "cache" the reference in an Init method like Start() or Awake().

private HorizontalAxis hAxis;

public void Start(){
    GameObject taxi = GameObject.Find("Taxi");
    if(taxi != null){ 
        hAxis = taxi.GetComponent<CarAgent>().HorizontalAxis;
    }
}

public void Update(){
   if(hAxis != null)
     transform.rotation = new Quaternion(0, 0, hAxis, 360);
}

public void FixedUpdate(){
    if(hAxis != null){
        // Do only Physics related stuff: rigidbodies, raycast
        // inputs and transform operations should stay in the Update()
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Omar Guendeli
  • 322
  • 1
  • 8