- Home /
How to reference a Game object in a different scene
Hi guys I am having some trouble referencing a Game Object
I need to have the Game Object referenced in this scene to allow financial calculations to happen. It exists in another scene (Home Page scene). I have used DontDestroyOnLoad to keep the same GameObject across all scenes.
This is the code I have for the U16/U19 Panel
public GameManager gamemanager;
public float BaseJuniorU16Cost;
public float BaseJunioru19Cost;
public float BaseMaintainceu19;
public float BaseMaintainceu16;
public Text U16Text;
public Text U19Text;
void Start () {
}
public void U16onclick ()
{
U16Text.text = "Enabled";
if (!gamemanager.CanBuy (BaseJuniorU16Cost))
return;
gamemanager.AddToBalance (-BaseJuniorU16Cost);
}
public void U19onclick ()
{
U19Text.text = "Enabled";
if (!gamemanager.CanBuy (BaseJunioru19Cost))
return;
gamemanager.AddToBalance (-BaseJunioru19Cost);
}
Should I make the GameManager private, and use Gameobject.find?
Answer by tormentoarmagedoom · Oct 09, 2017 at 08:11 AM
Hello !!
Using GameObject.Find("Name") is not the best performance solutiuon. It will work, but is not a good practice use it. Instead, is better to use GameObject.FindWithTag which is ebtter for performance, but goth solution are correct.
Find the object and modyfy/read all variables you need.
Answer by MAYALS18 · Jan 17, 2018 at 07:38 AM
Replace the code:
private GameManager gamemanager;
public float BaseJuniorU16Cost;
public float BaseJunioru19Cost;
public float BaseMaintainceu19;
public float BaseMaintainceu16;
public Text U16Text;
public Text U19Text;
void Start () {
gamemanager = GameObject.Find("GameManager");
}
public void U16onclick ()
{
U16Text.text = "Enabled";
if (!gamemanager.CanBuy (BaseJuniorU16Cost))
return;
gamemanager.AddToBalance (-BaseJuniorU16Cost);
}
public void U19onclick ()
{
U19Text.text = "Enabled";
if (!gamemanager.CanBuy (BaseJunioru19Cost))
return;
gamemanager.AddToBalance (-BaseJunioru19Cost);
Answer by fafase · Jan 17, 2018 at 10:43 AM
If your object exists in another scene and you only need the data it contains, you could just save them and discard your object.
Using DontDestroyOnLoad also means you need to make sure you don't end up with two when you get back to the scene.
Best is to save them with PlayerPrefs
void ResetValue(string key, string value){
PlayerPrefs.SetString(key, value);
}
Answer by Harinezumi · Jan 17, 2018 at 10:57 AM
It seems you need the ubiquitous and somewhat-evil singleton pattern:
public class GameManager : MonoBehaviour {
private static GameManager instance = null; // static (class level) variable
public static GameManager Instance { get { return instance; } } // static getter (only accessing allowed)
private void Awake () {
// if instance is not yet set, set it and make it persistent between scenes
if (instance == null) {
instance = this;
DontDestroyOnLoad(gameObject);
}
else {
// if instance is already set and this is not the same object, destroy it
if (this != instance) { Destroy(gameObject); }
}
}
...
}
This way you can refer to GameManager
by writing GameManager.Instance
.
Your answer
Follow this Question
Related Questions
Can't re-enable a gameobject 1 Answer
Disable GameObject through Prefab? 2 Answers
Grid Layout Group leaking outside canvas. 1 Answer
Which type of Variable do I need to use to change the Value of a text UI object ? 0 Answers
Stumped on object not set to an instance of an object, even though it is!? 1 Answer