#Desperate Fight with a NRE

1 messages · Page 1 of 1 (latest)

umbral spoke
#

NullReferenceException: Object reference not set to an instance of an object
QueenPheromone.Start () (at Assets/_Scripts/AntBehavior/Pheromones/QueenPheromone.cs:24)
PheromoneMachine.Initialize (System.Collections.Generic.List`1[T] pheromones) (at Assets/_Scripts/AntBehavior/PheromoneMachine.cs:34)
Ant.AssignCaste (Caste caste) (at Assets/_Scripts/AntRelated/Ant.cs:107)
Colony.InitializeQueen () (at Assets/_Scripts/AntRelated/Colony.cs:89)
Colony.Awake () (at Assets/_Scripts/AntRelated/Colony.cs:36)
UnityEngine.Object:Instantiate(GameObject)
World:CreateColony() (at Assets/_Scripts/World.cs:90)
World:Create(Vector3) (at Assets/_Scripts/World.cs:38)
GameManager:Start() (at Assets/_Scripts/Managers/GameManager.cs:24)

prime saffron
umbral spoke
#

Sorry I'm working on it

#

It's very complex. I thought I had it all

prime saffron
#

Just give the one script with the error for now

#

Pasting them all into one paste isn't that helpful since we lose line numbers

#

Include everything so the line numbers line up

umbral spoke
prime saffron
#

You cut out the using directives

umbral spoke
prime saffron
#

And the end of the script

umbral spoke
#

Yes, sorry, I meant to close the brackets, the other parts aren't relevant

prime saffron
#

The error is here
Debug.Log("Machine: " + machine.ToString())

#

this means machine is null

umbral spoke
#

Yes I know the error, I've traced it all the way back. That's why I was including the other scripts

#

But I have been searching for hours on why its null

#

lol

prime saffron
#

This means _ant.AntBehaviorMachine; is null

umbral spoke
#

correct

#

So the sequence goes: Colony calls InitializeQueen() which creates a new Queen which calls Ant.OnStart

prime saffron
#

Good chance that Ant.OnStart hasn't run yet

#

Or you overrided that and forgot to call the base function

umbral spoke
#

Queen.Start calls base.OnStart()

#

It is in the first link

#

So is the OnStart method

prime saffron
#

That will run before Start

#

So Queen.Start won't have run yet

umbral spoke
#

But then why has the error-throwing code run??

prime saffron
#

Even more so because _queen = Instantiate(_queenPrefab).GetComponent<Queen>() is happening

umbral spoke
#

Bc the way I see the code, that code shouldn't get run until Queen.Start goes

prime saffron
prime saffron
#

Note that's not a Monobehaviour start it's just a function you're calling called Start

umbral spoke
#

Oh let me include the PheromoneMachine code

prime saffron
#

Not really needed

#

Seems the error is clear now to me

umbral spoke
#

But that is the script that actually calles QueenPheromone.Start

prime saffron
#

So?

umbral spoke
#

or am I not seeing a line of code that calls QueenPheromone.Start?

prime saffron
#

The stack trace already shows that

umbral spoke
#

I don't see that

prime saffron
#

So I don't need to see the code

#

I know it's happening

umbral spoke
prime saffron
umbral spoke
#

Yes, I've been scouring them up and own for the source of the problem LOL

prime saffron
#

I just pointed out though?

umbral spoke
#

lol that does not mean i understand

prime saffron
#

The machine isn't assigned until Start runs on the Queen

#

You're calling a function on the Queen that assumes it has already been called

#

Before it does

umbral spoke
#

oh i think I understand now

prime saffron
#
_queen = Instantiate(_queenPrefab).GetComponent<Queen>();
        _queen.AssignColony(this);
        _queen.AssignCaste(casteQ);
umbral spoke
#

So this is not a monobehavior, which means start doesn't get called

#

which is what you said

prime saffron
#

No

#

The opposite

umbral spoke
#

oh it's still a monobehavior for inheriting from ant?

prime saffron
#

You are calling that start function manually inside AssigbCaste

umbral spoke
#

so Start does get called when it is created

prime saffron
#

But Start runs later

#

No

#

Awake does

#

And OnEnable

#

Start runs much later

umbral spoke
#

After all awakes?

prime saffron
#

More like, right before its first Update

#

Could even be next frame

umbral spoke
#

AH including after Colony's awake??

#

got it

prime saffron
#

Other scripts aren't really relevant here

umbral spoke
#

jesus fuck

prime saffron
#

You're instantiating the queen

umbral spoke
#

I shouldn't be doing it in Awake?

prime saffron
#

By the time Instantiate returns, Awake and OnEnable on the Queen will have run

#

So you can assume they have run right after Instantiate

#

But Start will not

#

You should just do the Queen's initialization in Awake or make a custom function that initializes it and call that after Instantiate

umbral spoke
#

My quick fix was to move the base.OnStart() call to Queen.Awake

#

The problem is that I have a large number of different objects that all need to be meshing together, and I think I need to organize that better.

#

Or time it better somehow