Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How to properly pass data between scenes?

Discussion in 'Scripting' started by chu65536, Jul 25, 2023.

  1. chu65536

    chu65536

    Joined:
    Nov 30, 2021
    Posts:
    17
    Basically i have scene 1 with scrollview selection menu with a bunch of buttons and scene 2 with action
    Scene 2 is always the same and it depends only on selection menu choice.

    What is the best way to pass selection value into the next scene?
    I read about Scriptable Objects and static variables but since i am new to Unity i dont know which one is better for my situtiation. So that is why i am here
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    I generally use something like a DDOL singleton which stores my game-wide session information and game state.

    Definitely not static variables.
     
  3. nontoxicguy1

    nontoxicguy1

    Joined:
    Apr 5, 2023
    Posts:
    8
    So, the answer highly depends on the context. I'll talk about static variables and scriptable objects in this answer. There's other ways to pass data between scenes but let's just talk about these now.

    Scriptable objects are made to store large amount of readonly data. Useful for objects that have different types but the same logic. Of course, you can change the data contained at runtime. They offer excellent readability and maintainability, which is beneficial for teams.

    On the other hand, we have static variables, a straightforward way. They're an easy way to get things done, but definitely not the best way to make it. For beginners or small solo projects, they're a convenient solution that may not scale well as the project expands.

    In summary, scriptable objects are a better choice. If you're making a very small solo game, static variables can be easier though. Keep in mind I only approached the two solutions you pointed out.
     
  4. chu65536

    chu65536

    Joined:
    Nov 30, 2021
    Posts:
    17
    I thought about passing array data of my own class to another scene at first, which cannot be done by using static variables as i understood (easiest approach in my opninion). So i started to read about other methods

    But know i am thinking about passing just a single variable which represents an index to this array, but array will be calculated in the main scene. I guess for this approach static variable is the best option
     
  5. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    431
  6. chu65536

    chu65536

    Joined:
    Nov 30, 2021
    Posts:
    17