Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

NullReferenceException in Steering.cs #11

Closed
mdeforge opened this issue Jan 17, 2013 · 3 comments
Closed

NullReferenceException in Steering.cs #11

mdeforge opened this issue Jan 17, 2013 · 3 comments

Comments

@mdeforge
Copy link

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
Copy link
Owner

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
Copy link
Author

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//issues/11#issuecomment-12358472.

@ricardojmendez
Copy link
Owner

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants