I have two scenes, scene 0 and scene 1. In scene 1 we have this code "
{
punchBoxCollider = punchBox.GetComponent<BoxCollider2D>();
spriteRenderer = GetComponent<SpriteRenderer>();
ourCollider = gameObject.GetComponent<BoxCollider2D>();
if (gameObject.name.Contains("Clone") == false)
{
UnityEngine.Debug.Log("the name of punchboxcollider is " + punchBoxCollider.name);
// sprite code
for (int i = 0; i < 22; i++)
{
if (i < 10)
{
// Debug.Log("1");
loadNumber = ("0" + i).ToString();
}
else
{
// Debug.Log("2");
loadNumber = i.ToString();
}
var sp = Resources.Load<Sprite>("BadGuySprites/sprite_" + loadNumber);
badGuySpriteDictionary.Add(i, sp);
}
if (basicEnemySpawnCounter == 0)
{
isThisMother = true;
isThisInstantiated = false;
}
}
else
{
spriteRenderer.sprite = badGuySpriteDictionary[UnityEngine.Random.Range(0, 22)];
}
}"
However Im getting an error when I load scene 1, that my badGuySpriteDictionary has already been filled up (error when trying to add a sprite at key 0). This tells me that this code has already been ran once.
Scene 1 is not loaded at the start of the game, only scene 0. My question is, should my code in this start function contained in scene 1 be running when i start my game? I can easily program around this, but I just want to know if it is intended functionality.
1 Like
In normal conditions, scripts on game objects are called only when scene was loaded, but there are mechanics to control it another way:
public class SceneUtilitySetup
{
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.AfterSceneLoad)]
static void Setup()
{
var gameObject = new GameObject("SceneUtility");
gameObject.AddComponent<SceneUtility>();
}
}
Due to not providing the full class, build settings, etc. it is hard to say, what’s going on.
Maybe you placed this code also in the scene 0.
You can delete this class, comment all related calls to hide errors if you have them in other places, and then check missing stuff all over the project with 3rd-party plugins like these.
Such way you can discover where this class can exist.
1 Like
Your post title asks a question about void start(); being called.
The method void start(); will NEVER be called by Unity.
The correct method you’re looking for is likely void Start();
Capitalization is critical to getting identifiers correct. start() has no meaning in Unity. Start() does have specific meaning. See the docs for more details.
Further, this statement:
Is just a bug you wrote. And that means… time to start debugging!
By debugging you can find out exactly what your program is doing so you can fix it.
Use the above techniques to get the information you need in order to reason about what the problem is.
You can also use Debug.Log(...); statements to find out if any of your code is even running. Don’t assume it is.
Once you understand what the problem is, you may begin to reason about a solution to the problem.
I see that you also do callsite string concatenation shenanigans like this:
These are cute but make it impossible to actually SEE the string you’re passing in, impossible to reason about its correctness.
Do your string concatenation to a temporary variable you can print out. Odds are that will help you find the bug.
I knew you guys would lock onto me not having start capitalized lmao. I fixed it about 1 second after posting😭. The issue is that i had my scenes being unloaded at the end of start, so that start was calling in everything. I think i’ve fixed this by moving the scene unload into my void awake(). Thanks for the help!
1 Like