I have a script that controls a score screen in the game. Its has some variables set, a start function where some calculations happen then an OnGUI() to display it all with some menu buttons. The problem I’m having is that Start() is being called like Update(), that is, it’s being called continuously. Am I missing something when I say Start only gets called once when the script is first loaded? Or, am I confusing Start() and Awake()?
I have checked to make sure Im not causing the loop myself and that doesn’t look to be the case.
{EDIT} Just a quick recap of what’s below. Start() is being looped for some unknown reason. The relevant code is below as well as an explanation of the game flow. Using Awake() shows the same recall/looping behavior. The 2 functions in Start()… AccuracyCal() and BonusCal()are not called anywhere else on the script or in other scripts. I need the Click and Clack Brothers of Unity to figure this one out.
3 Answers
3
Here is the explanation straight from the Unity script reference:
"Start is called just before any of the Update methods is called the first time.
Start is only called once in the lifetime of the behaviour. The difference between Awake and Start is that Start is only called if the script instance is enabled. This allows you to delay any initialization code, until it is really needed. Awake is always called before any Start functions. This allows you to order initialization of scripts.
The Start function is called after all Awake functions on all script instances have been called."
If you have multiple instances of this script on other gameobjects each of those object’s Starts will also be called. Maybe this is whats causing it?
perhaps you are continuously loading that scene over and over…
try adding this to debug:
function OnLevelWasLoaded (level : int) {
print ("Woohoo");
}
If the object that this script is on is being instantiated then it will run again, but other than that it will only run once. Try Awake() and see if that fixes it.
First, are you sure Start is called multiple times? Put a Debug.Log inside the Start function. If it is called multiple times then it's very likely that you call it multiple times. Start is a normal function it is only called once by Unity. You can call it manually as often as you like. If you select a Debug.Log entry in the console it shows a stacktrace so you can see from where it has been called.
– Bunny83Ok, it never gets to the end of start... put an output at the beginning of Start and one on the last line of Start... it never leaves the BonusCal() function. But there is nothing in the function that would cause it to loop. And its only ever used (called once) and that is in Start().
– WestonDOk, some edits. I had collapse on. It appears that after it finishes the bonus calculation it recalls Start(). Because I am seeing that debug output on the first line of start increment up but never the last debug line just after BonusCal(). Edit, I was wrong.. it is finishing start and placing the last debug line. It just gets buried in the output. So Start is looping for some reason, back to square 1.
– WestonDI swear Im not crazy. I thought it might have something to do with the Array but a simple test script ruled that out.
– WestonDAnyone else wanna take a crack at this?
– WestonD