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. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Can I Get Help

Discussion in 'Scripting' started by ShaunChad99, May 6, 2020.

  1. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    Hello I have got a game where i enter a house and that takes me to a different scene for the inside of the house and out. but when i leave the house it restarts my "world" scene and puts me where i start the game. is there a way to spawn my Player outside the house i have left?

    this is the code i am using.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class OnTriggerLoadScene : MonoBehaviour
    6. {
    7.     public GameObject guiObject;
    8.     public string sceneToLoad;
    9.  
    10.         void Start()
    11.     {
    12.         guiObject.SetActive(false);
    13.     }
    14.  
    15.     void OnTriggerStay (Collider other)
    16.     {
    17.         if (other.gameObject.tag == "Player")
    18.         {
    19.             guiObject.SetActive(true);
    20.             if (guiObject.activeInHierarchy == true && Input.GetButtonDown("Use"))
    21.             {
    22.                 Application.LoadLevel(sceneToLoad);
    23.             }
    24.         }
    25.     }
    26.     void OnTriggerExit()
    27.     {
    28.         guiObject.SetActive(false);
    29.     }
    30. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    If you unload the scene, then reload it, everything in the scene will be by default reset.

    If you want to avoid this, then don't unload the outer scene: just put it entirely under a single GameObject and mark that GameObject as inactive while you're in the house, then turn it back on when you come out, unloading the house scene. You can additively load scenes and specifically unload particular scenes.

    The biggest thing to note is when you load a scene, it is not in memory until the next frame, which means you have to use some type of a persistent MonoBehavior and coroutine to wait one frame, then do things like repositioning the player in the newly-loaded scene.

    If you have a specific spawn place for the player, you can make that dynamic and have a game manager kinda script (google for tutorials on this) that will track where you left a scene such that when you go back it can restore your position there.

    I highly recommend you make two painfully simple scenes and tinker with only those two scenes until you get it right. It's very fiddly, but once you have it set up, it's very powerful.
     
  3. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    Do you know any good tutorials??
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,971
    Most tutorials are focused on a single small thing. There may be ones that focus on additive loading, or inter-level linking, I just don't know any.

    Like I said, break it down to two tiny scenes, one the world, one your house, and then make JUST the linkages between them.

    I personally break all my games into multiple scenes, all additively loaded. For instance, I have a "playing" scene that contains virtually nothing, just a black "LOADING" overlay and a script that knows how to load everything else additively. That script then loads:

    - the content scene
    - a lighting rig scene
    - the enemy overlay for that scene and that level of difficulty
    - the player HUD
    - the player scene

    Finally when all the scenes are loaded, the LOADING overlay is deleted and the level begins.
     
  5. ShaunChad99

    ShaunChad99

    Joined:
    Apr 23, 2020
    Posts:
    26
    Okay thank you will have a go now