Context:
In my game, the player has a flashlight (spotlight on the camera) that runs out of juice with time, in which he must complete a certain task.
When he looses, i call an Application.LoadLevel(“looseMenu”), and on that scene i have a restart button.
When i restart the level, the light intensity value is the same as the one that was left when the player had lost previously, even though my start method states that the light intensity should be maxed.
I have ran a debug.log on the start method, and confirmed that it doesn’t get called after Application.LoadLevel() if it has already been called before (for the first time).
How do i get it to be called every time i reload the scene?
Here is an excerpt of my class which is attached to an object that also has a spotlight:
Public class flashlightController : MonoBehaviour {
private GUIStyle style = new GUIStyle();
public float fadeoutTime = 180.0F;
public float maxLightIntensity = 0.3F;
public float minLightIntensity = 0.1F;
float fade;
// Use this for initialization
void Start () {
light.intensity = maxLightIntensity;
}
// Update is called once per frame
void Update () {
if (light.intensity > minLightIntensity){
fade = fadeoutTime-Time.time;
light.intensity = fade/(fadeoutTime/maxLightIntensity);
}
}
}
Any help would greatly apprecieted! (i’m sure the solution is obvious and this is a noob question :p)
EDIT: Apparently i am an idiot, the Start method does run, i just had the debug window set to collapse. The “light.intensity = maxLightIntensity;” thing still doesnt work though (which is wierd, since the Debug.Log() is running fine. I’ll investigate a bit more
Update: Also, apparently all of my other methods that interact with the light don’t work.
Update2: Figured it out. Answer in the comments below.
I'm going to guess it's because you never actually unloaded the level. Try putting Debug.Logs inside some other events like OnDisable, OnEnable, OnDestroy This will give you more information about where in the life cycle your object actually is. I'd recommend putting your light.intensity setting in a Reset() function and run that from OnEnable and/or Start
– smoggachI have figured it out. Apparently Time.time doesn't reset after Application.LoadLevel()!!
– Pedro_Johnston