- Home /
Access FOV from another scene
I want to be able to change the FOV from another scene with the slider. Is this possible to do?
Answer by Simon-Larsen · Jan 18, 2015 at 11:13 PM
You could save a variable in your PlayerPrefs and load that value once your new scene loads. From your first scene you could save your value, doing something like this
PlayerPrefs.SetFloat("FOV", valueToSave);
And then fetch it from your other scene, doing something like this
PlayerPrefs.GetFloat("FOV", defaultValue);
This will load any float value saved with the key "FOV", however you should always fill in a default value. Just in case no value was ever saved.
PlayerPrefs.SetFloat("FOV", Camera.main.fieldOfView);
Camera.main.fieldOfView = PlayerPrefs.GetFloat("FOV", 60.0f);
60 is the default FOV used by Unity, so if you haven't saved the FOV yet, it will use 60.
having issues: the camera wont change fov. This is what I have:
void Start(){
//fov
Camera.main.fieldOfView = PlayerPrefs.GetFloat("FOV", 60.0f);
FOV.value = Camera.main.fieldOfView;
}
void Update () {
FOVTxt.text = "" + FOV.value;
}
public void FOVSlider(float fovSlider){
Camera.main.fieldOfView = fovSlider;
PlayerPrefs.SetFloat("FOV", Camera.main.fieldOfView);
PlayerPrefs.Save();
}
Answer by rmccabe82 · Jan 19, 2015 at 07:45 AM
Another option would be to make the slider value change set the value of a static variable somewhere. This static value would then be accessible from any other script later.
You won't be able to access this value across scenes, unless you perserve the gameobject. And if you do that, you'll save the components value anyways. You also won't be able to save the value between sessions.
Im pretty sure I've passed values from one scene to another using static variables.
If you setup a static object in one script and set its value in one scene, you can still access the set value it from another when it loads. Its static, it will remain for the lifespan of your game.
Something along the lines of this maybe?
public class ScriptA : $$anonymous$$onoBehaviour {
public void sliderChanged(float sliderVal)
{
Debug.Log(sliderVal);
ScriptB.FovValue = sliderVal;
}
}
public class ScriptB : $$anonymous$$onoBehaviour {
public static float FovValue;
void Start()
{
// When the screen loads, set the FOV to the value of the static variable.
Camera.main.fieldOfView = ScriptB.FovValue;
}
}
Ah yes, you're correct. Still won't save the value between sessions though.
True, but the question was how to modify an object in one scene using the value from another, not persistence when the game reloads ;-)
Your answer

Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Add GUI elements to Scene View? 3 Answers
Deactivate game modes until tutorial is finished? 1 Answer
Access loading screen scene components from Main Menu? 1 Answer