ricardojmendez / UnitySteer

Steering, obstacle avoidance and path following behaviors for the Unity Game Engine
https://numergent.com/tags/unitysteer/
Other
1.21k stars 276 forks source link

NullReferenceException in Steering.cs #11

Closed mdeforge closed 11 years ago

mdeforge commented 11 years ago

I was getting a NullReferenceException error in SteerToFollow.cs because I hadn't instantiated a Vehicle when trying to call CalculateForce() in SteerToFollow.cs, which of course inherits from Steering.cs.

protected override Vector3 CalculateForce() { //Debug.Log(Vehicle); // Was returning null return Vehicle.GetSeekVector(Target.position, _considerVelocity); //Thus this didn't work then obviously }

I went over into Steering.cs and changed the Vehicle GET property to the following and it finally worked:

// Cached vehicle
Vehicle _vehicle;
public Vehicle Vehicle {
    get { return _vehicle == null ? this.GetComponent<Vehicle>() : _vehicle; }
}

I've really studied your code and I'm not sure why calling the base to Start, and I even tried calling the base to Awake, didn't find the component before CalculateForce tried asking for it. I imagine I'll have this same problem if in the other behaviors. Any ideas? And can you reproduce this error?

ricardojmendez commented 11 years ago

Hi,

I'm not sure what you mean by this:

I've really studied your code and I'm not sure why calling the base to Start, and I even tried calling the base to Awake, didn't find the component before CalculateForce tried asking for it.

Do you mean that there was a Vehicle instantiated, and that the Awake for the Steering was being called, but for some reason _vehicle was still null the first time CalculateForce was invoked?

mdeforge commented 11 years ago

If by instantiated you mean via the Autonomous vehicle component I put on my game object, then yes, precisely.

I guess I should that I'm not using UnitySteer in it's out of the box state, but this is one part that I didn't touch. If you're not having the same error, I'd at least check into whether or not you're getting any sort of null exception before it successfully grabs the component.

Out of curiosity, could what order I attach the scripts to the game object matter?

On Thu, Jan 17, 2013 at 3:23 AM, Ricardo J. Méndez <notifications@github.com

wrote:

Hi,

I'm not sure what you mean by this:

I've really studied your code and I'm not sure why calling the base to Start, and I even tried calling the base to Awake, didn't find the component before CalculateForce tried asking for it.

Do you mean that there was a Vehicle instantiated, and that the Awake for the Steering was being called, but for some reason _vehicle was still null the first time CalculateForce was invoked?

— Reply to this email directly or view it on GitHubhttps://github.com/ricardojmendez/UnitySteer/issues/11#issuecomment-12358472.

ricardojmendez commented 11 years ago

Out of curiosity, could what order I attach the scripts to the game object matter?

It shouldn't, unless you're instantiating them via code at run-time. The queue that ticks the vehicles is created on TickedVehicle.OnEnable, which means that even an over-eager tick should happen after all Awakes have been called.

If you're instantiating them manually, and instantiate the Vehicle after the Steering, then I suppose it is possible.

I'm afraid I haven't seen this issue, and I do use it daily (it's what powers the characters on Hairy Tales). Do let me know if you manage to narrow it down!