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)
#Desperate Fight with a NRE
1 messages · Page 1 of 1 (latest)
You didn't even include the script in which the error is happening
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
You cut out the using directives
And the end of the script
Yes, sorry, I meant to close the brackets, the other parts aren't relevant
The error is here
Debug.Log("Machine: " + machine.ToString())
this means machine is null
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
This means _ant.AntBehaviorMachine; is null
correct
So the sequence goes: Colony calls InitializeQueen() which creates a new Queen which calls Ant.OnStart
Good chance that Ant.OnStart hasn't run yet
Or you overrided that and forgot to call the base function
Queen.Start calls base.OnStart()
It is in the first link
So is the OnStart method
InitializeQueen() happens in Awake
That will run before Start
So Queen.Start won't have run yet
But then why has the error-throwing code run??
Even more so because _queen = Instantiate(_queenPrefab).GetComponent<Queen>() is happening
Bc the way I see the code, that code shouldn't get run until Queen.Start goes
Because you're calling it
From this stack trace you can see QueenPheromone.Start is being called as a result of Colony.Awake()
Note that's not a Monobehaviour start it's just a function you're calling called Start
Oh let me include the PheromoneMachine code
But that is the script that actually calles QueenPheromone.Start
So?
or am I not seeing a line of code that calls QueenPheromone.Start?
The stack trace already shows that
I don't see that
Yeah but the way I read the code, it seems like Ant.OnStart should get balled before Queen.AssignCaste?
Look at the stack trace you shared. It shows the whole stack of functions being called
Yes, I've been scouring them up and own for the source of the problem LOL
I just pointed out though?
lol that does not mean i understand
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
oh i think I understand now
_queen = Instantiate(_queenPrefab).GetComponent<Queen>();
_queen.AssignColony(this);
_queen.AssignCaste(casteQ);
So this is not a monobehavior, which means start doesn't get called
which is what you said
oh it's still a monobehavior for inheriting from ant?
You are calling that start function manually inside AssigbCaste
Queen is
so Start does get called when it is created
After all awakes?
Other scripts aren't really relevant here
jesus fuck
You're instantiating the queen
I shouldn't be doing it in Awake?
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