Hello, my game has many scenes and I put these scenes into main menu. However, if player didn’t play that scene, player shouldn’t enter the scene in main menu gallery section.
This is my Singleton script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public abstract class Singleton<T> : MonoBehaviour where T : Component
{
#region Fields
/// <summary>
/// The instance.
/// </summary>
private static T instance;
#endregion
#region Properties
/// <summary>
/// Gets the instance.
/// </summary>
/// <value>The instance.</value>
public static T Instance
{
get
{
if ( instance == null )
{
instance = FindObjectOfType<T> ();
if ( instance == null )
{
GameObject obj = new GameObject ();
obj.name = typeof ( T ).Name;
instance = obj.AddComponent<T> ();
}
}
return instance;
}
}
#endregion
#region Methods
/// <summary>
/// Use this for initialization.
/// </summary>
protected virtual void Awake ()
{
if ( instance == null )
{
instance = this as T;
DontDestroyOnLoad ( gameObject );
}
else
{
Destroy ( gameObject );
}
}
#endregion
}
galleryMenuChecker.cs (Singleton in main menu)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class galleryMenuChecker : Singleton<galleryMenuChecker>
{
public bool fromGallery1 = false;
public bool fromGallery2 = false;
public bool fromGallery3 = false;
public bool fromGallery4 = false;
public void DirectScene1()
{
if (GameManager.Instance.Scene1AnimationUnlocked == false)
{
Debug.Log("You haven't played this scene yet!");
}
else if (GameManager.Instance.Scene1AnimationUnlocked == true)
{
GameObject.Find("LevelLoader").GetComponent<LevelLoader>().Scene1();
fromGallery1 = true;
}
}
public void DirectScene2()
{
if (GameManager.Instance.Scene2AnimationUnlocked == false)
{
Debug.Log("You haven't played this scene yet!");
}
else if (GameManager.Instance.Scene2AnimationUnlocked == true)
{
GameObject.Find("LevelLoader").GetComponent<LevelLoader>().Scene2();
fromGallery2 = true;
}
}
}
And my Game Manager (Singleton).
This one has booleans only.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class GameManager : Singleton<GameManager>
{
// Scene 4 Key is Taken or Not?
public bool S4_Keycard = false;
// Unity Scene Animations Unlocked or Not?
public bool Scene1AnimationUnlocked = false;
public bool Scene2AnimationUnlocked = false;
public bool Scene3AnimationUnlocked = false;
public bool Scene4AnimationUnlocked = false;
}
When the player play scene1, I execuete this command in Start():
GameManager.Instance.Scene1AnimationUnlocked = true;
So far so good, everything works fine. Buttons works fine for once. But when I complete the playing of scene1 and return to main menu with ESC pause menu, button no more works.
I checked the situation in edit mode and Singletons works fine, booleans are updated, yet button click does not work.
What did I wrong? Or is there any better way to make a gallery unlocker in Unity? Thank you.
More images: https://imgur.com/a/xa0jHrE