Boolean set to true, false in Update() function because of Start()
I am having an issue where I set a boolean value in one function and is being overwritten by the time i need to use it in another function. For context, this is an NPC Core for a game I am developing. The player has the ability to set any of the pieces to start first (which subsequently can also be an NPC) and that is where my problem occurs. here are the scenarios:
Scenario 1 If a Player piece is selected to move first, everything works fine and the following pieces, including NPCs, all work properly.
Scenario 2 If an NPC piece is selected to go first, the NPC never moves because StartWait is still false for whatever reason.
Using Debug.Log and a little bit of debugging knowledge, I have tracked the issue down to being this. In scenario 2 from above, after I set the boolean, it appears Start() is running and setting the variable back to false. I know this because the output in the console says "Waffles Toaster" instead of "Toaster Waffles".
How could I go about correcting this so start does not override my previous assignment?
// Initialization
void Start()
{
Debug.Log("Toaster");
NPCCoreVars.Timer = 0f;
NPCCoreVars.StartWait = false;
NPCCoreVars.ChosenPiece = null;
NPCCoreVars.ChosenCell = null;
NPCCoreVars.MoveOperation = 0;
NPCCoreVars.CurrentNPCTeam = TeamChoices.None;
}
// Used to control the timer
void Update()
{
// Are we waiting?
if (NPCCoreVars.StartWait == false) return;
// Decrement the timer
NPCCoreVars.Timer -= Time.deltaTime;
// Have we passed the timer?
if (NPCCoreVars.Timer <= 0)
{
// Reset timer variables
NPCCoreVars.StartWait = false;
NPCCoreVars.Timer = 0;
// Call the move function
DoNPCMove();
}
}
// The core function that decides what piece moves and where
public void NPCMakeMove(TeamChoices NPCTeam)
{
...
if (NPCCoreVars.ChosenPiece != null && NPCCoreVars.ChosenCell != null)
{
NPCCoreVars.Timer = 2f;
NPCCoreVars.StartWait = true;
Debug.Log("Waffles");
}
}
the console says Toaster because Start() function is called only once it will not updated every frame.
Answer by Bunny83 · Apr 08, 2016 at 10:45 AM
Well, it seems that you instantiate / enable your object and immediately call "NPCMakeMove" on it. That call will of course happen before Start is called since Start is delayed until the start of the next frame.
You should use "Awake" instead. Awake is called immedialtely when you enable the object. So if the prefab is already enabled, Awake will be called immediately when you Instantiate the object. If the prefab is disabled Awake will be called immediately when you enable it.
Apart from using Start or Awake you can always implement your own "Init" method and call it yourself at the right time.
Your answer
![](https://koobas.hobune.stream/wayback/20220612074417im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Trying to give instantiated object a value 0 Answers
unity doesnt work 0 Answers
Start function dont work 1 Answer
Problem with rotation to mouse position 0 Answers
Making a function that executes once change things gradually 1 Answer