Home>

Thank you for browsing.

I'm new to Unity.
I tried google, checked with the reference URL, and tried various things, but I could not solve it, so I asked a question.
This is my first question, so I'm sorry if there is a lack of explanation.

Premise

・ Title scene

Two buttons are set, and when you press each button, it is created with Canvas
・ How To Play ・ ・ ・ A button to return to the first screen (Title Object) is installed)
・ Difficulty level selection screen (Stage Select) ・ ・ ・ A button to move to the scene of the selected difficulty level and a button to return to the first screen (Title Object) are installed.

Is now displayed.

・ Game scene (Easy) ・ ・ ・ The difficulty level created at this stage is Easy only.

Created with Canvas as well as clear or game over
・ Clear screen (Clear Object) ・ ・ ・ A button to retry and a button to return to the title scene are installed)
・ Game Over screen (Game Over) ・ ・ ・ A button to retry and a button to return to the title scene are installed)

Is displayed.

I want to achieve
  1. Click the difficulty level selection of the title scene → Difficulty level selection screen → Easy → Clear screen or game over screen → Title scene ...

  2. Click the retry button on the clear screen or game over screen → Play the game and display the clear screen or game over screen again → Play the game again with the retry button ...

Regarding the content of what I want to achieve

・ If i press the button from the difficulty level selection screen in step 1 and move to Easy, the game over screen will be displayed and loaded. Perhaps one of the reasons is that when I moved to EasyScene, life went to 0 and the game over flag went from false to true (see code below).
Also, at this time, pressing the retry button and the return to title scene button does not respond.

-After playing the game again after retrying in 2, the button to return to Title and the retry button do not respond, and it is not possible to move to the corresponding scene and screen respectively. It seems that OnClick is not working well after the second time.

The URL I referred to is here.
Introduction to Unity [Unity usage course that even beginners can make from scratch]
https://dkrevel.com/makegame-beginner/

Corresponding source code
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
using UnityEngine.EventSystems;
public class GameManager: MonoBehaviour
{
    #region // Set in Inspector
    [Header ("GameManager")] public static GameManager instance = null;
    [Header ("Life")] public int life;
    [Header ("Number of goals 1 target")] public int target1CountScore;
    [Header ("Number of goals 2 goals")] public int target2CountScore;
    [Header ("Clear Variable")] public bool isClear = false;
    [Header ("Gameover variable")] public bool isGameover = false;
    [Header ("TitleObject")] public GameObject TitleObject;
    [Header ("HowToPlay")] public GameObject HowToPlay;
    [Header ("StageSelect")] public GameObject StageSelect;
    #endregion
    # region // private variables
    private Rigidbody2D rb;
    private bool isHowToPlay = false;
    private bool isStageSelect = false;
   // private bool isEasyScene;
    #endregion
    // Start is called before the first frame update
    private void Awake ()
    {
        if (instance == null)
        {
            instance = this;
            DontDestroyOnLoad (this.gameObject);
        }
        else else
        {
            Destroy (this.gameObject);
        }/ * DontDestroyOnLoad (TitleObject);
        DontDestroyOnLoad (HowToPlay);
        DontDestroyOnLoad (StageSelect);* /
    }
    void Start ()
    {
        // State when the title scene is loaded
        TitleObject.SetActive (true);
        HowToPlay.SetActive (false);
        StageSelect.SetActive (false);
        rb = GetComponent<Rigidbody2D>();
        // life when Easy is loaded
        if (SceneManager.GetActiveScene (). name == "Easy")
        {
            life = 5;
        }/* else if (SceneManager.GetActiveScene (). name == "Normal")
        {
            LoadNormalScene ();
        }
        else if (SceneManager.GetActiveScene (). name == "Hard")
        {
            LoadHardScene ();
        } * /
    }
    // Update is called once per frame
    void Update ()
    {
    }
    #region // retry
    public void RetryGame ()
    {
        target1CountScore = 0;
        target2CountScore = 0;
        isClear = false;
        isGameover = false;
        if (SceneManager.GetActiveScene (). name == "Easy")
        {
            LoadEasyScene ();
            life = 5;
        }/* else if (SceneManager.GetActiveScene (). name == "Normal")
        {
            LoadNormalScene ();
        }
        else if (SceneManager.GetActiveScene (). name == "Hard")
        {
            LoadHardScene ();
        } * /
    }
    #endregion
    #region // Move to title scene
    public void LoadTitleScene ()
    {
        if (SceneManager.GetActiveScene (). name == "Easy")
        {
            SceneManager.LoadScene ("TitleScene");
        }
    }
    #endregion
    #region // Easy load
    public void LoadEasyScene ()
    {
        SceneManager.LoadScene ("Easy");
        if (isStageSelect)
        {
            isStageSelect = false;
        }}
    #endregion
   /* public void LoadNormalScene ()
    {
        SceneManager.LoadScene ("Normal");
    }
    public void LoadHardScene ()
    {
        SceneManager.LoadScene ("Hard");
    } * /
    #region // Move to StageSelect screen
    public void GoStageSelect ()
    {
        TitleObject.SetActive (false);
        isStageSelect = true;
        StageSelect.SetActive (true);
    }
    #endregion
    #region // Go to HowToPlay screen
    public void GoHowToPlay ()
    {
        TitleObject.SetActive (false);
        isHowToPlay = true;
        HowToPlay.SetActive (true);
    }
    #endregion
    # region // Move to the first screen of the title scene
    public void GoTitleObject ()
    {
        TitleObject.SetActive (true);
        if (isHowToPlay)
        {
            isHowToPlay = false;
            HowToPlay.SetActive (false);
        } else if (isStageSelect)
        {
            isStageSelect = false;
            StageSelect.SetActive (false);
        }
    }
    #endregion

}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class LifeTextScript: MonoBehaviour
{
    #region // Set in Inspector
    public GameObject GameOver;
    #endregion
    # region // private variables
    private Text lifeText;
    private int oldLife;
    #endregion
    // Start is called before the first frame update
    void Start ()
    {
        lifeText = GetComponent<Text>();
        GameOver.SetActive (false);
        if (GameManager.instance! = null)
        {
            lifeText.text = "Life" + GameManager.instance.life;
        }
        else else{
            Destroy (this);
        }
    }
    // Update is called once per frame
    void Update ()
    {
        if (oldLife! = GameManager.instance.life)
        {
            lifeText.text = "Life" + GameManager.instance.life;
            oldLife = GameManager.instance.life;
        } else if (GameManager.instance.life<= 0)
        {
            lifeText.text = "Life 0";
            GameOver.SetActive (true);
            GameManager.instance.isGameover = true;
        }
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Target1GoalScript: MonoBehaviour
{
    #region // Set in Inspector
    [Header ("Number until goal")] public int Target1Score;
    [Header ("Count Target1")] public int Target1Count;
    [Header ("Count Target2")] public int Target2Count;
    [Header ("ClearObject")] public GameObject ClearObject;
    #endregion
    // Start is called before the first frame update
    void Start ()
    {
        ClearObject.SetActive (false);
    }
    // Update is called once per frame
    void Update ()
    {
    }
    #region // Processing when the goal is reached
    private void OnTriggerEnter2D (Collider2D collision)
    {
        if (collision.gameObject.CompareTag ("Burden"))
        {
            //Debug.Log ("Target1Goal");
            Destroy (collision.gameObject);
            GameManager.instance.target1CountScore + = Target1Score;
            if (GameManager.instance.target1CountScore>= Target1Count&&gameManager.instance.target2CountScore>= Target2Count)
            {
               //Debug.Log ("12Ok");
                ClearObject.SetActive (true);
                GameManager.instance.isClear = true;
            }
        }
    }
    #endregion
}

* In addition to Targe1GoalScript, there is also Target2GoalScript with almost the same content.

-It has been confirmed that all OnClick () of Button are set.
-The Build Setting also included Title Scene and Easy.
-When I read from Easy and returned to TitleScene, I could not move to HowToPlay or StageSelect.

I'm a beginner, so I'm sorry for the poor code, but I would appreciate your solutions and improvements.

Supplementary information (FW/tool version, etc.)

Windows 10
Use Unity 2020.4.1f1

  • Answer # 1

    I am late.

    I managed to solve it by the following method while checking it myself again.
    Thank you everyone!

    Create a new empty TitleSceneGameObjectCtrl in the title scene.

    Create the following script

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    public class TitleSceneGameObjectCtrlScript: MonoBehaviour
    {
        [SerializeField] GameObject TitleObject;
        [SerializeField] GameObject HowToPlay;
        [SerializeField] GameObject StageSelect;
        // Start is called before the first frame update
        void Start ()
        {
            GoBackToTitle ();
        }
        // Update is called once per frame
        void Update ()
        {
        }
        #region // Move to StageSelect screen
        public void GoStageSelect ()
        {
            StageSelect.SetActive (true);
            HowToPlay.SetActive (false);
            TitleObject.SetActive (false);
        }
        #endregion
        #region // Go to HowToPlay screen
        public void GoHowToPlay ()
        {
            HowToPlay.SetActive (true);
            StageSelect.SetActive (false);
            TitleObject.SetActive (false);
        }
        #endregion
        public void GoBackToTitle ()
        {
            TitleObject.SetActive (true);
            HowToPlay.SetActive (false);
            StageSelect.SetActive (false);
        }
    }

  • Answer # 2

    I can only say from the symptoms
    For the time being, please enter the initial value and try it to check if there is no problem at life 5. I don't think this will suddenly become Game Over.

    [Header ("Life")] public int life = 5;


    Next, there are various places
    if (SceneManager.GetActiveScene (). name == "Easy")
    It doesn't seem to be in the branch of.
    For the time being, please try using the Start method to see if it goes through Debug.log.
    If it doesn't pass, the scene name comparison isn't working.

     void Start ()
        {
            // State when the title scene is loaded
            TitleObject.SetActive (true);
            HowToPlay.SetActive (false);
            StageSelect.SetActive (false);
            rb = GetComponent<Rigidbody2D>();
            // life when Easy is loaded
            if (SceneManager.GetActiveScene (). name == "Easy")
            {
    Debug.log ("Set Life");
                life = 5;
            }

    After that, it seems to be useless unless you load the scene after setting the life to 5 with the retry method.

    #region // retry
        public void RetryGame ()
        {
            target1CountScore = 0;
            target2CountScore = 0;
            isClear = false;
            isGameover = false;
            if (SceneManager.GetActiveScene (). name == "Easy")
            {
    Life = 5;
                LoadEasyScene ();
        }