• 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 /
avatar image
Question by Blankets_McGee · Sep 23, 2020 at 05:31 AM · 2d-platformercollectible-game-objects

Checking Separate ScoreManager values in another Script to load certain levels.

Hello all! First I'd like to preface this with saying that this is for a jam so ultimately it's not detrimental to be answered but I would like some input nonetheless. What I'm trying to do is have two separate collectables with different score managers in my game and then depending on if either collectables value is higher than the other it will load a different level. I have it loading the correct levels based on the values however I definitely know there is a better and more optimized approach than mine. Here are the separate ScoreManager scripts:

 public class FutureScoreManager : MonoBehaviour
 {
 
     public static FutureScoreManager instance;
     public TextMeshProUGUI FutureText;
     public int FutureScore;
 
     // Start is called before the first frame update
     void Start()
     {
         if (instance == null)
         {
             instance = this;
         }
     }
 
     public int ChangeFutureScore (int FutureValue)
     {
         FutureScore += FutureValue;
         FutureText.text = "x" + FutureScore.ToString();
         return FutureScore;
     }
 }


Here is the MemoryScoreManager:

 public class MemoryScoreManager : MonoBehaviour
 {
 
     public static MemoryScoreManager instance;
     public TextMeshProUGUI MemoryText;
     public int MemoryScore;
 
     // Start is called before the first frame update
     void Start()
     {
         if (instance == null)
         {
             instance = this;
         }
     }
 
     public int ChangeMemoryScore (int MemoryValue)
     {
         MemoryScore += MemoryValue;
         MemoryText.text = "x" + MemoryScore.ToString();
         return MemoryScore;
     }
 }
 

then I have a collider at the end of the level set to OnTrigger and I have a script attached to the player with an OnColliderTrigger2D:

 private void OnTriggerEnter2D(Collider2D other) 
     {
         if (other.gameObject.CompareTag("PickUp"))
         {
             MemoryScoreManager.instance.ChangeMemoryScore(MemoryValue);
             Destroy(other.gameObject);
         }
 
         if (other.gameObject.CompareTag("PickUp2"))
         {
             FutureScoreManager.instance.ChangeFutureScore(FutureValue);
             Destroy(other.gameObject);
         }
 
         if (other.gameObject.CompareTag("LevelTrigger"))
         {
             if (MemoryObj.GetComponent<MemoryScoreManager>().ChangeMemoryScore(MemoryValue) > FutureObj.GetComponent<FutureScoreManager>().ChangeFutureScore(FutureValue))
             {
                 SceneManager.LoadScene(GoodSceneToLoad);
             }
 
             if (FutureObj.GetComponent<FutureScoreManager>().ChangeFutureScore(FutureValue) > MemoryObj.GetComponent<MemoryScoreManager>().ChangeMemoryScore(MemoryValue))
             {
                 SceneManager.LoadScene(BadSceneToLoad);
             }
         }
     }

This is working however I'm still very new to game dev in general and try to learn as much as possible. I researched scriptable objects as that seems like it might be a good approach? but I'm still trying to walk the logic around in my head and it's not landing very well lol. Any input is appreciated!!! Also, I apologize if the formatting of the post is incorrect. I think I used the code tags properly but if it's janky I'm sorry and will try to fix it.

Comment
jadeshine

People who like this

1 Show 0
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

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by UTus · Sep 23, 2020 at 05:44 AM

Use Singleton Pattern

EX) Singleton.Instance.Action();

 public class SingletonMonoBase<T> :MonoBehaviour where T :MonoBehaviour
 {
     private static T instance = null;
     private static object _synobj = new object();
     private static bool appIsClosing = false;
 
     public static T Instance
     {
         get
         {
             if (appIsClosing) return null;
 
             lock (_synobj)
             {
                 if(instance == null)
                 {
                     T[] objs = FindObjectsOfType<T>();
 
                     if (objs.Length > 0) instance = objs[0];
                     if (objs.Length > 1) Debug.LogError("There is more than one" + typeof(T).Name + "in the scene.");
 
                     string goName = typeof(T).ToString();
                     GameObject go = GameObject.Find(goName);
 
                     if (go == null) go = new GameObject(goName);
                     instance = go.AddComponent<T>();
                 }
                 return instance;
             }
         }
     }
 
 
     private void Awake()
     {
         StartCoroutine(OnAwakeCoroutine());
     }
     protected virtual IEnumerator OnAwakeCoroutine()
     {
         yield return null;
     }
 }
Comment
jadeshine

People who like this

1 Show 0 · 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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

146 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

Related Questions

How to make slider joint immovable by the player 1 Answer

Can someone help me with the jumping in this Player Controller script? 1 Answer

Quick question. 2 Answers

Can pixel perfect camera can be done with different PPU assets? 0 Answers

Problems With Adding Objects to a list 2 Answers


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