Is Something Wrong With My Singleton Pattern?
Hello all,
I will post my scripts then explain my predicament. Here is my first script, DynamiteMover, that has the Singleton Pattern in it:
using UnityEngine;
using System.Collections;
public class DynamiteMover : MonoBehaviour
{
public float speed;
public float rotSpeedY;
private GameManagerScript GMS;
//NOTE:
//Singleton Pattern used from line 15 - 27
static DynamiteMover _instance;
public static DynamiteMover instance
{
get
{
if (_instance == null)
{
_instance = FindObjectOfType<DynamiteMover>();
}
return _instance;
}
}
void Start ()
{
GMS = GameObject.Find ("GameManager").GetComponent<GameManagerScript> (); //Finds the script "GameManager"
}
public float acceleration;
void Update ()
{
if (GMS.countDownDone == true)
{
speed += Time.deltaTime * acceleration; //Set what "speed" is
transform.position += Vector3.back * speed; //Moves the gameobject
transform.Rotate (0.0f, rotSpeedY, 0.0f); //Rotates the gameobject
}
}
}
And here is most of my second script, DynamiteCollision, where my problem lies:
using UnityEngine;
using System.Collections;
public class DynamiteCollision : MonoBehaviour
{
public AudioSource scream;
private ScoreManager scoreManagerScript;
void Start()
{
scoreManagerScript = GameObject.Find("ScoreManager").GetComponent<ScoreManager> ();
scream = GetComponent<AudioSource>();
}
IEnumerator BriefPause ()
{
yield return new WaitForSeconds (2.0f);
Application.LoadLevel (7);
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.tag == "Player")
{
scoreManagerScript.scoreIncreasing = false;
scream.Play();
PlaneMover.instance.scrollSpeed = 0.0f;
PlaneMover.instance.acceleration = 0.0f;
DynamiteMover.instance.speed = 0.0f;
//StartCoroutine(BriefPause());
}
}
My problem lies inside of the OnTriggerEnter function, on the second to last line of code, just above the CoRoutine I commented out. Nothing happens whenever the Dynamite enters the trigger (which is my player). What I want to happen is for the Dynamite to stop moving, but it does not stop.
Is there something wrong with the way I set up my Singleton Pattern? Or is the something wrong with the way I called it in my DynamiteCollision script?
EDIT: The singleton pattern works on my "PlaneMover" script that stops when the trigger is entered. The singleton is exactly the same on the PlaneMover and DynamiteMover scripts and yet the Dynamite won't stop, that is why I am puzzled and asking here on Unity answers.
This should work if there is exactly one Dynamite in the scene from the beginning to end.
I think the pattern is a bit weird, because following my own way of thinking both of those scripts would be attached to the Dynamite and the static stuff would not be necessary, because you could just use GetComponent(). That approach would work with multiple Dynamites as well.
Sorry for the late response! But okay I tried the "GetComponent()" and it stops the dynamite that hits the player, but the other dynamites clones co$$anonymous$$g down my map do not stop. Why is that? @ScaniX
I cannot say by looking at just those scripts.
GetComponent<Dynamite$$anonymous$$over>().speed = 0.0f;
Should set the speed to 0, but at the same time, your other script increases it again. I don't know the logic behind that or why there are two scripts on the dynamite anyway.
It is the same with every problem: To find out what is going on, add some printouts. Check if your code is executed at all and what exactly it does. You can also pause the game and check the instances to see if the colliders are there and enabled, etc.
The typical way of using singletons is instantiating them ASAP (most often in the Awake() method)
private static Dynamite$$anonymous$$over _instance;
public static Dynamite$$anonymous$$over instance
{
get { return _instance;) }
}
void Awake()
{
if (_instance != null)
{
DestroyImmediate(gameObject);
} else
{
_instance = this;
}
}
Answer by felixpk · Jul 22, 2016 at 06:32 PM
The proper way of using singletons in Unity is like this:
public class SingletonClass : MonoBehaviour {
private static SingletonClass instance;
void Awake() {
instance = this;
}
public static SingletonClass Instance {
get {
return instance;
}
}
}