- Home /
Script Execution Order Question
Hello everyone, I've got a question about the execution of a script I made that deals with when values applied from the inspector are executed.
The problem: I made a script that requires that that bools be checked for the script to spawn certain prefabs in the scene. The script (in Awake) then adds prefabs based on the selections into a List. However, reguardless of the bool status, all prefabs are added into the list (and debug statements show that the script considers all bools to be true even if none actually are).
This is solved by moving the script into Start, but I was wondering what the logic behind the scenes was.
The script in question:
void Awake ()
{
prefabNumberArray = new List<GameObject>();
if (useRockPrefab1 == true)
{
Debug.Log ("adding rock1Prefab" + useRockPrefab1);
prefabNumberArray.Add (rock1Prefab);
}
if (useRockPrefab2 == true)
{
Debug.Log ("adding rock2Prefab" + useRockPrefab2);
prefabNumberArray.Add (rock2Prefab);
}
if (useRockPrefab3 == true)
{
Debug.Log ("adding rock3Prefab" + useRockPrefab3);
prefabNumberArray.Add (rock3Prefab);
}
if (useRockPrefab4 == true)
{
Debug.Log ("adding rock4Prefab" + useRockPrefab4);
prefabNumberArray.Add (rock4Prefab);
}
}
Interesting. I just made a script:
public bool b1, b2, b3, b4;
void Awake()
{
if (b1)
{
Debug.Log("1");
}
if (b2)
{
Debug.Log("2");
}
if (b3)
{
Debug.Log("3");
}
if (b4)
{
Debug.Log("4");
}
}
If I check any of the 4 bools in the inspector, when I click play, it only prints out the number that was selected, which I believe is the intended behavior. This leads me to wonder, how do you declare your booleans?
This https://docs.unity3d.com/Documentation/$$anonymous$$anual/ExecutionOrder.html and this http://docs.unity3d.com/Documentation/Components/class-ScriptExecution.html should answer your question.
@perchik: below is the variable declarations that relate to the bools and list. Also, I noticed that in debug, the prefabs are being added: 3,4,1,2 for some reason.
public class SpawnFloatingRocks : $$anonymous$$onoBehaviour {
public bool useRockPrefab1;
public bool useRockPrefab2;
public bool useRockPrefab3;
public bool useRockPrefab4;
public GameObject rock1Prefab;
public GameObject rock2Prefab;
public GameObject rock3Prefab;
public GameObject rock4Prefab;
public List<GameObject> prefabNumberArray;
So, does this script work the "right way" for you?
test.cs:
using UnityEngine;
using System.Collections;
public class test : $$anonymous$$onoBehaviour {
public bool b1;
public bool b2;
public bool b3;
public bool b4;
void Awake()
{
if (b1)
{
Debug.Log("1");
}
if (b2)
{
Debug.Log("2");
}
if (b3)
{
Debug.Log("3");
}
if (b4)
{
Debug.Log("4");
}
}
}
Answer by unimechanic · Feb 21, 2014 at 08:40 PM
Question solved according to comments, please summarize your solution as an answer for future reference. Adding this answer to remove it from the Unanswered view and give more relevance to other questions requiring attention. Thanks for your comprehension, Unity Support.