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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Pass object to new Scene On Load

Discussion in 'Scripting' started by mholmes, Oct 11, 2018.

  1. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    I have seen lots of samples but non of them seem to make sense or at least give the full solution. I have a object "Log_In" and I want to pass it to my new scene on load. Here is my code:

    Code (CSharp):
    1. if (!_Response)
    2.         {
    3.             _Core.Message_Box("Log In Error", "Log In Failed! Please Check Your User Name Or Password", "OK");
    4.         }
    5.         else
    6.         {
    7.             //Load Main Menu & Pass Object Log_In
    8.             SceneManager.LoadScene("Main Menu");
    9.         }
    Looking for a simple & clean way to pass the object Log_In into the Main Menu scene.
     
  2. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    You can use DontDestroyOnLoad() to prevent a specific GameObject from being removed during a scene transition. If you have some data you need to preserve, you can put it in a script attached to that GameObject.

    You can also preserve data by putting it in a static variable somewhere.
     
  3. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    Error:
    Severity Code Description Project File Line Suppression State
    Error CS1503 Argument 1: cannot convert from 'WBG.Log_In' to 'UnityEngine.Object' Bugology C:\Users\mholmes\Documents\GitHub\Bugology\Assets\WBG\Scenes\Log In\Scripts\btnLogIn.cs 76 Active

    code:
    Code (CSharp):
    1.  //Load Main Menu & Pass Object Log_In
    2.             DontDestroyOnLoad(login);
    3.             SceneManager.LoadScene("Main_Menu", LoadSceneMode.Single);
     
  4. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    So "login" is clearly not a GameObject. What is it, then? A MonoBehaviour? A POCO?

    How are you intending to access it once the new scene loads?
     
  5. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    Log_In is a class from my Dll.


    Code:
    Code (CSharp):
    1. private void Event_Log_In(Log_In login)
    2.     {
    3.         string uri = _EndPoint + "api/WhiteBoXGaming/Post_UserLogIn";
    4.  
    5.         //HTTP Request
    6.         StartCoroutine(booleanwebrequest(uri, login));
    7.  
    8.         if (!_Response)
    9.         {
    10.             _Core.Message_Box("Log In Error", "Log In Failed! Please Check Your User Name Or Password", "OK");
    11.         }
    12.         else
    13.         {
    14.             //Load Main Menu & Pass Object Log_In
    15.             SceneManager.LoadScene("Main_Menu", LoadSceneMode.Single);
    16.         }
    17.     }

    Also getting this error for my scenes everytime i try to go to a different scene:

    Scene 'Create_Account' couldn't be loaded because it has not been added to the build settings or the AssetBundle has not been loaded.
    To add a scene to the build settings use the menu File->Build Settings...
    UnityEngine.SceneManagement.SceneManager:LoadScene(String, LoadSceneMode)
    btnCreateAccount:Event_LoadCreateaccount() (at Assets/WBG/Scenes/Log In/Scripts/btnCreateAccount.cs:10)
    UnityEngine.EventSystems.EventSystem:Update()

    edit:
    fixed issue with scenes not loading but still dont know how to move that data across scenes
     
    Last edited: Oct 11, 2018
  6. Antistone

    Antistone

    Joined:
    Feb 22, 2014
    Posts:
    2,833
    Since you don't seem to have understood anything I've said so far, I guess I am unable to help you.
     
  7. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
     
    mholmes likes this.
  8. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    Sorry I'm at work so I have to skim through posts and I did not get a chance to read your responses clearly. What I would like to do in a perfect world would be pass it in via constructor. In the .Net world I could pass it in as a parameter in the constructor. Can we do that in Unity? I hope that makes sense
     
  9. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    Great video so it really raises the question of which is best to use? I honestly come from a .Net background so I favor Static over the other methods. However, I don't want to back my self in a corner when it comes to getting help etc. What are the pro's & con's of static vs. Do Not Destroy? From what I seen static really seems to be the best solution but I want to keep an open mind and hear what others suggest and why they prefer one over the other.

    So what method does everyone else prefer and why? Whats the advantage?
     
  10. Lurking-Ninja

    Lurking-Ninja

    Joined:
    Jan 20, 2015
    Posts:
    9,900
    I prefer data-holding ScriptableObjects myself. They're basically assets which does not belong to any scenes but can be referenced from any of them.

    Close second the data-holding static classes (although obviously because of the drawbacks of the static patterns you have to be careful not to pollute your global too much).

    I personally do not like the DonDestroy method because I don't like the 'manager'-game objects at all.
    They don't belong there. And after you have 5-6 scenes you will make mistakes in the chaos of the dondestroyonloads.
     
    Last edited: Oct 12, 2018
    mholmes likes this.
  11. mholmes

    mholmes

    Joined:
    Dec 8, 2012
    Posts:
    407
    Yeah I think that's where younger devs fail when using/building static classes and I'm guilty as well trying to build everything into a single class when i first started. Something I learned when building static classes that stuck with me was someone described a class like a bone in your body. Your class is one building block of many that make up your application. Each piece needs to hold values for a certain part of the application. Some how it made sense to me and I just stuck with that concept.