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

КОМЕНТАРІ • 56

  • @jw7994
    @jw7994 2 роки тому +8

    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.

  • @happym0nk3y
    @happym0nk3y 3 роки тому +2

    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.

  • @andrewsemenenko8826
    @andrewsemenenko8826 Рік тому

    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!

  • @thellamaamanda1246
    @thellamaamanda1246 2 роки тому +1

    DUDE you are a life saver, thank you very much. Hoping you're having a good time. Keep it going.

  • @Alex-hf6zy
    @Alex-hf6zy 2 роки тому +1

    didnt realise it was that simple! Thanks for this

  • @Bonnie39
    @Bonnie39 2 роки тому +8

    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)

    • @Jourlik
      @Jourlik 2 роки тому +5

      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

    • @t3333o
      @t3333o Рік тому

      @@Jourlik thank you i love you sm

  • @oliviad2582
    @oliviad2582 2 роки тому

    thank you i love you this helped me make a working score system that saved the score during the game over scene

  • @NeleN_Games
    @NeleN_Games 3 роки тому

    i like this so much cuz finaly i understood what static is doing . ty man

  • @Luvseatshawty
    @Luvseatshawty 3 роки тому +2

    Thanks for his video. Very simple and very helpful!

  • @NLPIsrael
    @NLPIsrael 9 місяців тому

    can you make a video on the same ussiu with netcode?... thanx for your work!

  • @boritotv6885
    @boritotv6885 2 роки тому +1

    thats exactly what i was looking for, you saved my ass

  • @oedipascourage6262
    @oedipascourage6262 2 роки тому

    Just what I needed. Thank you so much!

  • @Luluskuy
    @Luluskuy Рік тому

    thanks bro, this was better and more performance than DontDestroyOnLoad methods.

  • @spectrumRoyale
    @spectrumRoyale 2 роки тому +1

    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

  • @hengpisey658
    @hengpisey658 3 роки тому

    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.

  • @tunailker8
    @tunailker8 Рік тому

    Thank you so much man, I really appreciate your help!

  • @bigmancozmo
    @bigmancozmo 2 роки тому

    thx i was making a battle system in my game but didn't want to make a million scenes for each battle

  • @YejunYun
    @YejunYun Місяць тому

    THANKS A LOT

  • @DanNguyenQuoc-mm2ot
    @DanNguyenQuoc-mm2ot 5 місяців тому

    Supercalifragilisticexpialidocious!!! It worked

  • @qqq9542
    @qqq9542 3 роки тому +1

    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?

  • @sajidgame1321
    @sajidgame1321 3 роки тому +1

    Very nice tutorial

  • @taiyanglu7214
    @taiyanglu7214 Рік тому

    Thanks so much for the tip!

  • @rp88imxoimxo27
    @rp88imxoimxo27 3 роки тому +2

    What's the use to inherit StateNameController from MonoBehaviour if you dont attach it to any gameObjects?

  • @biki_02
    @biki_02 Рік тому

    thanks a lot for this bro.❤

  • @bandoleiro431
    @bandoleiro431 2 роки тому

    Thank you so much!

  • @omegaomega6838
    @omegaomega6838 2 роки тому

    Super helpful thanks

  • @kasunlee7489
    @kasunlee7489 3 роки тому +1

    Cool. Thx man.

  • @manavarora7644
    @manavarora7644 3 роки тому

    Thank you so much !!!!

  • @alvintan1226
    @alvintan1226 3 роки тому +1

    Great tutorial easy to understand!

  • @MichealHolt
    @MichealHolt Рік тому

    You know, I'm a little bit embarrassed I didn't think of static variables...

  • @berkayylmaz2527
    @berkayylmaz2527 3 роки тому

    thx bro

  • @untemi4045
    @untemi4045 3 роки тому

    thanks

  • @sajidgame1321
    @sajidgame1321 3 роки тому

    Nice video, but how about if we using playerprefs? & wich one is better? or wich one should we use?

    • @unitygameprogrammingforbeg9319
      @unitygameprogrammingforbeg9319  3 роки тому +6

      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.

    • @sajidgame1321
      @sajidgame1321 3 роки тому

      @@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.

  • @zhangjiaxi9948
    @zhangjiaxi9948 2 роки тому

    nice

  • @BrodyBrodester
    @BrodyBrodester Рік тому

    So this is essentially global state in unity? Awesome.

    • @512Squared
      @512Squared 5 місяців тому +1

      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.

    • @BrodyBrodester
      @BrodyBrodester 5 місяців тому +1

      @@512Squared Thank you for the information. I did end up doing what you suggested. But have since migrated my project to JS/React

  • @GamePlay-dh5mb
    @GamePlay-dh5mb Рік тому

    This Is Not What The Title Said
    The Title Said Between Scenes Not Between Scripts 🤦‍♂🤦‍♂

  • @sripranav
    @sripranav 3 роки тому

    We don't have to reference statenamecontroller? Im getting errors!

    • @512Squared
      @512Squared 5 місяців тому

      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.

  • @dimitristripakis7364
    @dimitristripakis7364 2 роки тому

    These videos make me think "why do we need schools for, nowadays ?".
    Jesus the world has changed. To the BETTER !!!

  • @andrejrevak404
    @andrejrevak404 Рік тому

    Sorry, but you misspelled the name of this tut. Its how to send variables between SCENES, not SCRIPTS. Its another thing yknow?

  • @WillToWinvlog
    @WillToWinvlog Рік тому

    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?

  • @Johan-rm6ec
    @Johan-rm6ec Рік тому

    Scripts not scenes.

  • @realkyrex
    @realkyrex Рік тому

    THERE IS NO THING LIKE STATENAME......BLABLABLA FUNCTION IN UNITY

  • @Rollbaa
    @Rollbaa Рік тому

    not scalable solution if you have more data to pass from scene to scene. Little hacky just for demo purposes

    • @unitygameprogrammingforbeg9319
      @unitygameprogrammingforbeg9319  Рік тому +1

      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.

  • @uchihamadara6045
    @uchihamadara6045 Рік тому

    bro SPEAK UP ICANT HEAR YOU

  • @itsWalter
    @itsWalter Рік тому

    thank you man, helped me a whole lot!