Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Load scene with param variable Unity

In my game there is a map view that contains a 50x50 grid of tiles. When you click on the tile you get sent to that tiles view and attack things, etc. The only difference between these "tiles" as far as the code will be concerned is the tiles ID, aka. which number on the grid. That number will be passed to the server on init to handle the rest.

Obviously, with that being the only difference in tiles it would be a mistake to create a scene "1", scene "2"...scene "2500" and call SceneManager.LoadScene to switch to the specific tile view.

I could use DontDestroyOnLoad(); when the tile is clicked to preserve the tile ID on scene switch but 1) it only accepts gameobjects not just an int variable 2) I don't need/want to preserve that variable for anything more than the init in tile view. So while it could work it seems like overkill.

Is there a better practice for essentially just passing a parameter to a scene load?

like image 975
DasBeasto Avatar asked Feb 22 '17 13:02

DasBeasto


People also ask

How to manage loading screen objects in Unity?

Then, when you want to load a new Scene, instead of directly Loading the target Scene, transition to the dedicated Loading Scene. Inside the Loading Scene, add a script to load the next level of the game as soon as the Loading Scene starts. Creating one special Loading Scene can be an easy way to manage Loading Screen objects in Unity.

What is the loading scene method in Unity?

When using multiple canvases in Unity, the Canvas sort order decides which will be displayed on top. The Loading Scene Method involves using a dedicated Scene to display the Loading Screen instead of an in-game object. This method is great for building really complex Loading Screens, with game tips, rotating 3D models, animated visuals etc.

How to add a scene to a Unity script?

Remember to add the Scene you want to load to the build, otherwise, none of this will work. Next, you’ll need to add the Scene Management namespace to any script that you want to load Scenes from. Simply add “using UnityEngine.SceneManagement;” with the other using directives. This will allow you to use functions from the SceneManager class.

How to make a variable a static variable in Unity?

Use the static keyword. Use this method if the variable to pass to the next scene is not a component, does not inherit from MonoBehaviour and is not a GameObject then make the variable to be static. Built-in primitive data types such as int, bool, string, float, double. All those variables can be made a static variable.


1 Answers

You can make a static class that holds the information for you. This class will not be attached to any GameObject and will not be destroyed when changing scene. It is static which means there can only be ONE of it; you can't write StaticClassName scn = new StaticClassName() to create new static classes. You access them straight through StaticClassName.SomeStaticMethod() for example and can be accessed from anywhere. See this example on how to store a value in a variable, change scene and then use it in that scene (please note the added using UnityEngine.SceneManagement;):

A normal Unity script attached to a GameObject in Scene "Test":

using UnityEngine;
using UnityEngine.SceneManagement;

public class TestingScript : MonoBehaviour 
{
    void Start()
    {
        StaticClass.CrossSceneInformation = "Hello Scene2!";
        SceneManager.LoadScene("Test2");
    }
}

A new static class (not inheriting from monobehaviour) that holds information:

public static class StaticClass 
{
    public static string CrossSceneInformation { get; set; }
}

A script attached to a game object in scene "Test2":

using UnityEngine;

public class TestingScript2: MonoBehaviour 
{
    void Start () 
    {
        Debug.Log(StaticClass.CrossSceneInformation);
    }
}

You don't need to have the entire class static (if you for some reason need to create more instances of it). If you were to remove the static from the class (not the variable) you can still access the static variable through StaticClass.CrossSceneInformation but you can also do StaticClass sc = new StaticClass();. With this sc you can use the class's non-static members but not the static CrossSceneInformation since there can only be ONE of that (because it's static).

like image 145
Fredrik Schön Avatar answered Oct 27 '22 00:10

Fredrik Schön