- Home /
Question by
levan1 · Feb 01, 2015 at 06:44 PM ·
gameobjectlightsaveactive
Save gameobject active
I have a scene with the light off, then I insert this script to turn the light on, i want it to be turned on after restarting the scene. C# using UnityEngine; using System.Collections;
public class LightOnOff : MonoBehaviour {
public Light camera1;
public Light camera2;
public Light camera3;
private int currCam = 0;
void Start (){
camera1.enabled=false;
camera2.enabled=false;
camera3.enabled=false;
currCam = 0;
}
void OnMouseDown(){
currCam = ( currCam + 1 ) % 2;
switch( currCam )
{
case 0 :
camera1.enabled=false;
camera2.enabled=false;
camera3.enabled=false;
break;
case 1 :
camera1.enabled=true;
camera2.enabled=true;
camera3.enabled=true;
break;
}
}
}
Comment
Without getting into details, if you want to store a variable (like the state of your lights), across "play" sessions, you can use PlayerPrefs. http://docs.unity3d.com/ScriptReference/PlayerPrefs.html
how to make it in the same scene? can you show me script like this?
void Start () {
currCam = PlayerPrefs.GetInt("GlurthValue");
}
void OnApplicationQuit()
{
PlayerPrefs.SetInt("GlurthValue", currCam );
}
Answer by Frenchie92 · Feb 01, 2015 at 08:54 PM
If you want the lights to be on after restarting the scene, your Start() function should not be setting all of the lights.enabled to false
Your answer
![](https://koobas.hobune.stream/wayback/20220613182148im_/https://answers.unity.com/themes/thub/images/avi.jpg)