1

I have main scene (street) and many child scenes (houses), to which I can come from the main scene. When I come to one home, then go back to the street, I need player object placed near that house, where he was. So I need to use DontDestroyOnLoad to remember his position.

enter image description here

But when I come at home, I need to disable player object from the main scene and here my code doesn't work. Also after first use of this script in simulation, I can't move cursor up /down, only left/right (and this is first person shooter)

public PS plr;

private GameObject playerOnMainScene;

public void Awake()
{
    PlayerSingleton();
    DisablePlayerOfMainScene();
}

void PlayerSingleton()
{
    if (!plr)
    {
        DontDestroyOnLoad(gameObject);
        plr = this;
    }
    else
        Destroy(gameObject);
}

void DisablePlayerOfMainScene()
{
    playerOnMainScene = GameObject.FindGameObjectWithTag("PlayerOnMainScene");

    if (SceneManager.GetActiveScene().name != "MainScene")
    {
        Debug.Log("1"); // this code doesn't work when scene name != "MainScene"
        playerOnMainScene.SetActive(false);
    }
    else
        playerOnMainScene.SetActive(true);
}
Amazing User
  • 3,473
  • 10
  • 36
  • 75
  • 1
    you MUST have a "preload scene" in every Unity project. it's just "one of those things they don't mention". Have your DDOL objects on there. – Fattie Mar 08 '16 at 22:53
  • 1
    there are no singletons in ECS game engines, it is a meaningless concept. (every GameObject ... is a singleton. if you "need a singleton" ... that is a GameObject.) – Fattie Mar 08 '16 at 22:53
  • 2
    note that to simply remember the position of an object, just have a trivial static class (NOT a MonoBehaviour, just a plain static - basically for "globals") – Fattie Mar 08 '16 at 22:54
  • 1
    Possible duplicate of [Unity singleton manager classes](http://stackoverflow.com/questions/13730112/unity-singleton-manager-classes) – Fattie Mar 08 '16 at 22:54
  • ok first. You don't need Don't Destroy OnlLoad to remember his position. Just save the XYZ location of your player transform. Second You don't Disable the player, you disable the Script that is controller the player. SetActive to false will not get your player back on Active if you didn't Store the pointing reference outside your Function call. Like your DisablePlayerMainScene it is trying to find the object again that is "Active"False already, it won't find it. – Aizen Mar 09 '16 at 03:57
  • Possible duplicate of [Unity game manager. Script works only one time](http://stackoverflow.com/questions/35890932/unity-game-manager-script-works-only-one-time) – Fattie Jul 03 '16 at 14:08

0 Answers0