Edit: Cant access light component after Application.LoadLevel()

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.

Do you have a DontDestroyOnLoad running around somewhere?

If so you can duplicate the effect of Start with OnLevelWasLoaded

Time.time is seconds since game start, did you tried using Time.deltaTime?

If you don’t have any DontDestroyOnLoad…

fade and fadeoutTime2 don’t think are necessary.

I changed your script to this:

public class flashlightController : MonoBehaviour {
     
    private GUIStyle style = new GUIStyle();
     
    public float fadeoutTime = 0.1F;
    public float maxLightIntensity = 0.3F;
    public float minLightIntensity = 0.1F;
     
    // Use this for initialization
    void Start () {

    light.intensity = maxLightIntensity;
     
    }
     
     
    // Update is called once per frame
    void Update () {
     
       if(light.intensity > minLightIntensity) {
          light.intensity -= Time.deltaTime * fadeoutTime;
       } else {
          // just to be sure it doesn't go below this
          light.intensity = minLightIntensity;
       }
     
    }
}