Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by stephen_george98 · Jul 22, 2016 at 11:37 AM · c#triggerspeedinstancesingleton

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.

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image ScaniX · Jul 22, 2016 at 12:14 PM 1
Share

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.

avatar image stephen_george98 ScaniX · Jul 24, 2016 at 10:57 PM 0
Share

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

avatar image ScaniX stephen_george98 · Jul 24, 2016 at 11:16 PM 1
Share

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.

avatar image CodeElemental · Jul 22, 2016 at 12:43 PM 0
Share

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;
             }
 }

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;
         }
     }
 }
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

194 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException: Object reference not set to an instance of an object in Singleton class. 1 Answer

i am using bicycle pro kit .. i want to boost up speed on trigger enter how can i do this ? add force not working in this kit. 0 Answers

List access from another script 1 Answer

Game object not enabled after SetActive(true). 1 Answer

How can i get the name of the gameobject that triggered a trigger? 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges