How do I transfer a float between two Scripts in two different Scenes?
I have extensively looked for solutions, but I have not found any solution that worked for me.
I have an options menu with a slider which affects the public float thrust
. I want this float to remain so it can be used in a different script in a different Scene. I tried the DontDestroyOnLoad
but it wants a game object not a float. How could I fix this?
Initial script
public class OptionsMenu : MonoBehaviour { static public float thrust; public void SetThrust(float thrust) { Debug.Log(thrust); }
Answer by Larry-Dietz · Nov 26, 2019 at 10:44 PM
The way I handle things like this is I create a script specifically for holding these variables, The script only contains public variables, and a start method with DontDestroyOnLoad, then I add that script to an empty game object in my startup scene.
Then in code in any other scene that needs these variables, find that script and read the variable..For example, assuming the script is called SelectedOptions, when I need to get a variable out if it I use the following...
SelectedOptions selectedOptions;
Void Start()
{
selectedOptions = FindObjectOfType<SelectedOptions>();
{
then anywhere in that script I need to access one of the variables, I just use selectedOptions,VariableName
Hope this helps, -Larry
Your answer
![](https://koobas.hobune.stream/wayback/20220612215319im_/https://answers.unity.com/themes/thub/images/avi.jpg)