How do I access Singleton Pattern Script in Unity3d C#
Okay. I got one script ' script in singleton pattern . I have an issue with the other script . I need to know what I need to change on the script call Game . I need to access the GameManagerSingleton from the game script is what I am saying. I know need to make some changes on it . I don't know how to approach this situation. I made some changes on the game script so I can access the singleton script . But I am getting a lot of errors. The game script access and keep track of the score and time . I just need the score code on the game script to be corrected. Here are my scripts :
using UnityEngine; using System.Collections; using UnityEngine.UI;
public class GameManagerSingleton : MonoBehaviour { public static GameManagerSingleton instance = null;
public int score = 0;
Text text;
void Awake()
{
if (instance != null && instance != this)
Destroy(gameObject); // Ensures that there aren't multiple Singletons
instance = this;
}
}
public class ScoreObject { void IncreaseScore() {
GameManagerSingleton.instance.score += pointsEarned;
}
void Update() {
text.text = "Score : " + score;
}
}
using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using System.Collections; public class Game : MonoBehavior {
private GameManagerSingleton GameManagerSingletonScript _ Instance
private myclock MyClockScript;
private void Start ()
{
GameManagerSingleton = GetComponent<GameManagerSingleton>();
myClock = GetComponent<MyClockScript>();
}
void Update ()
{
if (myClock != NULL && GameManagerSingleton != NULL)
{
// need somthing here
}
if (m_leftTime >= 0 && 50 < GameManagerSingleton.score)
{
SceneManager.LoadScene("time");
}
}
}
format the code and proofread your question. It hardly makes sense.
Answer by Jessespike · May 17, 2016 at 05:58 PM
One of the features of singletons is that you can access the public static instance. There is no need for GetComponent and storing it in a separate class. You simply access the singleton with GameManagerSingleton.instance
.
Is that a class inside of a class? Why is ScoreObject inside of GameManagerSingleton? How can IncreaseScore() use pointsEarned, if that pointsEarned isn't declared anywhere? Well, I'll ignore that because you only asked about Game script.
public class Game : MonoBehavior {
private myclock MyClockScript;
private void Start ()
{
myClock = GetComponent<MyClockScript>();
}
void Update ()
{
if (myClock.m_leftTime <= 0 || GameManagerSingleton.instance.score > 50)
{
SceneManager.LoadScene("time");
}
}
}
Use "null", not "NULL". it's case-sensitive
No need to store a reference of GameManagerSingleton
m_leftTime is not declared anywhere, I'm assuming that's a property from MyClockScript.
GameManagerSingleton.score
should beGameManagerSingleton.instance.score
I don't know what conditions you want for loading the "time" scene, but I'm going to guess you want to switch when time runs out or when the score is above 50, I'm not sure though, there are no comments describing the condition.
When writing scripts, it's better to write a little bit, compile it, check for errors and fix them immediately. Instead of writing 100s of lines and trying to fix all the errors at once.
Your answer
Follow this Question
Related Questions
How do I use Singleton Design pattern in unity3d C# 2 Answers
Singleton Problem 0 Answers
Something Is Breaking And I Don't Know What 0 Answers
i need help with my board manager coding ASAP 1 Answer
Help with Script 0 Answers