- Home /
Help setting up a game manager
I have some game modes where once the player picks one, it shows up in the level with the game mode that you picked. But this script only works on the first level. My question would be: Would making a game manger game object in my hierarchy then putting the text game objects on level 2 in the game manager work? And would the script for the game manager look similar to this? Not sure how the script should be set up.
Because I am going to need the game mode name to appear on level 2 once you clear level 1, but the below script only gets it to appear on level 1.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
//Goes to level 1 after clicking Endurance Mode
//"Endurance Mode" will show up on level 1 when this button is clicked
public class endurance : MonoBehaviour
{
public GameObject EnduranceText;
void Start()
{
EnduranceText.SetActive(false); //start endurance text in level disabled
}
public void EnduranceMode()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex + 2);
if (EnduranceText.activeSelf)
{
EnduranceText.SetActive(true);
}
else
{
EnduranceText.SetActive(true);
}
Answer by Aviryx · Aug 20, 2020 at 10:59 PM
Update 2
I don't see why you need 3 separate text elements... that is the whole point of passing data through an SO. You can populate the text at runtime.
using UnityEngine;
using UnityEngine.UI;
public class Level1Manager : MonoBehaviour
{
public GameDataScriptableObject gameData;
public Text gameMode;
void Start()
{
gameMode.text = gameData.gameMode;
}
}
using UnityEngine;
using UnityEngine.UI;
public class Level2Manager : MonoBehaviour
{
public GameDataScriptableObject gameData;
public Text gameMode;
void Start()
{
gameMode.text = gameData.gameMode;
}
}
using UnityEngine;
using UnityEngine.UI;
public class Level3Manager : MonoBehaviour
{
public GameDataScriptableObject gameData;
public Text gameMode;
void Start()
{
gameMode.text = gameData.gameMode;
}
}
Update 1
In answer to your comment... it depends. You could have a script that you attach to the gameobjects in question (the ones that need to display the text) which could look like this: (I dont get how you are using gameobects to display text tbh... are you using a canvas attached to the gameobject with a UI/text component?
using UnityEngine;
public class DisplayGameMode : MonoBehaviour
{
public GameDataScriptableObject gameData;
void Start()
{
Debug.Log(gameData.gameMode);
}
}
But really you should be able to use my examples to make your own working example unless you can be more clear about how you are displaying text (you say get gameobjects to display text... are you using UI/Text/Canvas?)
1) Create this script and then right click in the assets folder > Create > Data > GameData. This will give you a scriptable object.
GameDataScriptableObject.cs
using UnityEngine;
[CreateAssetMenu(fileName = "GameData", menuName = "Data/GameData", order = 1)]
public class GameDataScriptableObject : ScriptableObject
{
public string gameMode;
}
2) you can reference this scriptable object like a class:
public class SomeClass : Monobehaviour
{
GameDataScriptableObject gameData;
}
3) once you have a reference you can get the game mode from any script that uses a reference.
gameData.gameMode;
----
I have read this about 5 times but am still unsure exactly what you are trying to achieve. You used mixed terminology (you talk about text but appear to be using game objects?)
I'm going to assume a few things for the sake of providing an example so hopefully I don't get too much wrong. You appear to want some kind of text (UI?) that shows what mode the user selected during individual scenes. The first thing I would say is that keeping scenes "separate" is a good practice. What I mean by this is a scene should not be dependant on data/stuff being loaded/kept from the previous scene - rather you should attempt to transition the data using a cleaner method.
You could use a scriptable object to store the text you want to display and then it's scene independent. For a scene/level to display the correct mode all you need to do is get the data from your SO. (unless I misunderstood what you wanted in which case this will most likely be useless)
MainMenuManager.cs
using UnityEngine;
using UnityEngine.UI;
public class MainMenuManager : MonoBehaviour
{
public GameDataScriptableObject gameData;
public string gameMode;
public Button startGame;
public Button selectMode1;
public Button selectMode2;
public Button selectMode3;
public GameObject warningMessage;
public Text noModeSelectedWarningMessage;
void Start()
{
noModeSelectedWarningMessage = " you have not selected a mode! ";
InitializeButtons();
}
private void StartGame()
{
if (gameMode != "")
{
gameData.gameMode = gameMode;
// load whatever scene
}
else
{
warningMessage.SetActive(true);
}
}
private void SetGameMode1()
{
gameMode = "Game Mode 1";
}
private void SetGameMode2()
{
gameMode = "Game Mode 2";
}
private void SetGameMode3()
{
gameMode = "Game Mode 3";
}
private void InitializeButtons()
{
startGame.onClick.AddListener(StartGame);
selectMode1.onClick.AddListener(SetGameMode1);
selectMode2.onClick.AddListener(SetGameMode2);
selectMode3.onClick.AddListener(SetGameMode3);
}
}
GameDataScriptableObject.cs
using UnityEngine;
[CreateAssetMenu(fileName = "GameData", menuName = "Data/GameData", order = 1)]
public class GameDataScriptableObject : ScriptableObject
{
public string gameMode;
}
Level1Manager.cs
using UnityEngine;
public class Level1Manager : MonoBehaviour
{
public GameDataScriptableObject gameData;
void Start()
{
Debug.Log(gameData.gameMode);
}
}
You pretty much nailed it. So, my first scene is the title screen, and theres a button that says "start game" I attached a script to it so you advance to the next scene after clicking it. Next scene you would press the "single player" button, then that goes to the Game $$anonymous$$odes scene, which are also buttons. Lets say you pressed the "Game $$anonymous$$ode 1" button, that brings you right to level 1 and it should say "Game $$anonymous$$ode 1" via a text object I have on levels 1 to...however many levels the game has.
So it should say it on each level.
Yeah so you should have everything you need in the example I gave? Have the button change the string used to store the game mode... and inside each level just have a script that accesses gameData.game$$anonymous$$ode
in order to get the game mode text/string;
"and inside each level just have a script that accesses gameData.game$$anonymous$$ode in order to get the game mode text/string;" will that be attached to my text objects in each level? I have one text object for each game mode name. And what would the script look like?