How to Send Variables Between Scenes in Unity
Вставка
- Опубліковано 3 вер 2024
- In this video we will be taking a look at how to send variables between scenes in Unity. Ultimately, sending variables between scenes isn't too different from sending variables between scripts in Unity.
Ultimately, what we want to do is make sure we have a centralized location where we can send those variables to temporarily stay while the new scene loads. Once that new scene loads, we can send variables between scenes by calling them back.
There are a few ways to accomplish sending variables between scenes. In this case, we'll be using a static string to transfer a difficulty setting. However, you can accomplish this same thing with enums or private non-static variables.
Try our game PORTAL GALAXY for Android here: bit.ly/PG_WCG
ZOMBIE BARREL BLAST signups here: bit.ly/WCG_ZBB
THANK YOU! Total beginner here and I was getting very frustrated. It felt like everything I tried to do ran into the issue of a script or scene not being able to access the information that it needed. This is going to make things so much easier.
Thanks for this, all of the other tutorials are the same copy paste setting of quality, resolution etc and don't handle making variables available for game objects, this is really useful.
Thank you very much !
I didn't even know how to properly express my problem to find the solution, but was ablr to find your video!
Great explanation, bravo!
DUDE you are a life saver, thank you very much. Hoping you're having a good time. Keep it going.
didnt realise it was that simple! Thanks for this
used this tutorial to make a basic options screen for my game, thank you! it finally works.
however, i have one question: how can i make the variables carry over when the player goes back to the main menu? (check boxes and sliders have the same values as set by the player)
idk if you still need this, but if anyone reads this here's a solution. When you create a static variable and set a value, it stays through all the scenes in that class (name of the script). So basically you just create a static variable, for example "int volume" and set it with whatever number you desire in options with ui. Then you just reference "Scriptname.volume" in the game itself, it's that easy
@@Jourlik thank you i love you sm
thank you i love you this helped me make a working score system that saved the score during the game over scene
i like this so much cuz finaly i understood what static is doing . ty man
Thanks for his video. Very simple and very helpful!
can you make a video on the same ussiu with netcode?... thanx for your work!
thats exactly what i was looking for, you saved my ass
Just what I needed. Thank you so much!
thanks bro, this was better and more performance than DontDestroyOnLoad methods.
And what about int, float and other because using static it will not allow you to change anymore so you need to manually assign it for example public static int coinsStatic;
public int coins;
void Start()
{
coinsStatic = coins;
}
But the problem is that how we can pass variables through scriptable object after using a singleton through other Mono behaviour scripts without attach them after scene change
thanks for this. I'm just looking for how to get user input and display in next scene but i saw your video and give me some hint.
Thank you so much man, I really appreciate your help!
thx i was making a battle system in my game but didn't want to make a million scenes for each battle
THANKS A LOT
Supercalifragilisticexpialidocious!!! It worked
Great video man, glad I subbed. Can I ask, can this method also be applied to collected coins shown in a level and in the main menu?
Very nice tutorial
Thanks so much for the tip!
What's the use to inherit StateNameController from MonoBehaviour if you dont attach it to any gameObjects?
thanks a lot for this bro.❤
Thank you so much!
Super helpful thanks
Cool. Thx man.
Thank you so much !!!!
Great tutorial easy to understand!
wrong
You know, I'm a little bit embarrassed I didn't think of static variables...
thx bro
thanks
Nice video, but how about if we using playerprefs? & wich one is better? or wich one should we use?
Player prefs is for saving variables between game sessions. So it all depends. Say you want to set the game difficulty to hard and then have it remember that the game is set to "hard" the next time you load it up, you would store that value in player prefs. If you want to manually set the difficulty every time (like I have done here) then there is no need to use player prefs.
@@unitygameprogrammingforbeg9319 Thanks alots for you, now I understand from your nice Reply & quick Reply. Thanks again, I understood much more than before, when after your Reply.
nice
So this is essentially global state in unity? Awesome.
Usually a variable belongs to an instance of the class it's declared in. So you might have multiple instances, each with its own instance ID. But this means two things, you only have one instance as such, but also that the variable belongs to the class, not to the instance, which is why you can call it without having to create and reference a specific instance of that class. But it does more than that, because if you have that variable and you do have multiple instances, then they will all use that one variable instance, they will share it across the instances, so change it in one instance and it will propagate across all the instances. It would make sense too to encapsulate that variable and thereby control access or impose constraints.
@@512Squared Thank you for the information. I did end up doing what you suggested. But have since migrated my project to JS/React
This Is Not What The Title Said
The Title Said Between Scenes Not Between Scripts 🤦♂🤦♂
We don't have to reference statenamecontroller? Im getting errors!
Yep. He didn't show this, but you need to attach that script to an object in each scene. That then becomes the instance used when your scene loads. Hence you can set different values like easy, medium and hard for that one value in each scene.
These videos make me think "why do we need schools for, nowadays ?".
Jesus the world has changed. To the BETTER !!!
Sorry, but you misspelled the name of this tut. Its how to send variables between SCENES, not SCRIPTS. Its another thing yknow?
That's my issue with Unity. Something that should be stupidly simple. SendVariableTo(Variable, Scene). It's such an essential function how the $$#% can't this basic function be in Unity, saving us all literally hours of extra work? Are they trying to weed out beginners intentionally?
U can use patterns for that event listener
Scripts not scenes.
THERE IS NO THING LIKE STATENAME......BLABLABLA FUNCTION IN UNITY
4:40 - the StateNameController is a script we create in this video.
not scalable solution if you have more data to pass from scene to scene. Little hacky just for demo purposes
A more scalable solution would be to use a scriptable object (or several depending on the size of your game) as "settings" objects and load them up with variables that you can have anywhere with a global scope. Another would be to wrap up variables in serialized classes and pass those around. Another would be to have a scene loaded in as additive that overlays your other scenes and gives you access to those objects. These are some of the things I do to move variables around on a larger scale.
bro SPEAK UP ICANT HEAR YOU
thank you man, helped me a whole lot!