- Home /
Get a float transferred between two scenes and scripts
I have two scenes, one where you type in the speed of a car via Input Field, and the next one, with a car with a script attached to it, with the float "speed".
Script Nr. 1:
public static class Data
{
private static int speed;
public static int Speed
{
get
{
return speed;
}
set
{
speed = value;
}
}
}
And Scipt Nr. 2:
public class Driving : MonoBehaviour {
float speed=0f;
}
I'm just a beginner and tried the whole day, my first try with script 1 was this:
public class Data : MonoBehaviour {
static public float speed=10f;
public InputField speedField;
void OnSubmit()
{
speed = float.Parse(speedField.text);
Debug.Log(speed);
}
}
But some guys on Srtack Overflow told me I should make it not this way. I don't understand where I have to attach the first script and how I transfer speed from scene 1 to scene 2 and from script Data to Driving. Thanks
Answer by toddisarockstar · Oct 03, 2018 at 04:33 AM
this is all you need:
public class SharedSavedStuff{
public static float sharedfloat;
}
I would put it OUTSIDE the monobehavior brackets. Or in a new script and get rid of the part that says" : MonoBehaviour"
you dont even need to attach it to anything!
the variable can be accessed from anywhere from any other script and it wont be affected by scene reloads and things like that.
you access it by saying: SharedSavedStuff.SharedFloat = 12674.3f;
Answer by tormentoarmagedoom · Oct 02, 2018 at 08:46 PM
Good day.
You should look some tutorials about data persistance, to transfer info between scenes.
https://unity3d.com/es/learn/tutorials/topics/scripting/persistence-saving-and-loading-data
Good bye.
Answer by NikunjAppIndia · Oct 03, 2018 at 05:15 AM
You should use ScriptableObject. Using ScriptableObject for Data architecture / sharing
Your answer
![](https://koobas.hobune.stream/wayback/20220612175643im_/https://answers.unity.com/themes/thub/images/avi.jpg)