Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by $$anonymous$$ · Jun 15, 2013 at 09:35 AM · c#referencesingletonpriority

Need suggestion to handle references in Singleton.

Hope to be clear on this question. I have several managers as singleton on my game that for the most part manage generic functions like playback of audio or save settings, but I have also one component that control player nad Ui functions for easy access. This is how the main functions to get the player references looks like:

 GameObject playerGo;
 [HideInInspector]
 public PlayerStatus Status;
 [HideInInspector]
 public PlayerController Controller;
 [HideInInspector]
 public PlayerWeapons Weapons;
 [HideInInspector]
 public UIPlayerManager UI;
 
 //Player Status values
 public int playerHealth;
 public int playerArmor;
 
 //Player Weapons values
 public int currentWeapon;
 
 public int[] currentAmmo; //0 is Fists, keep it at 1
 public bool[] weaponLock;
 
 //Setup Player
 void Awake (){InitManager();}
 
 //Failsafe
 void Start (){InitManager();}
 
 //Failsafe to load components

void OnLevelWasLoaded(int level) {InitManager();}

 //Setup components
 void InitManager () {  
     if(GameObject.FindWithTag("Player")) {
         playerGo = GameObject.FindWithTag("Player");
         Status = playerGo.GetComponent();
         Controller = playerGo.GetComponent();
         Weapons = playerGo.GetComponent();
     }
     if(GameObject.FindWithTag("UITag")) {
         UI = GameObject.FindWithTag("UITag").GetComponent();;
     }
 }

As you can see the manager get the player references on awake, then it tries also OnLevelWasLoaded as failsafe and on Start too. I did put another failsafe to be sure everything is loaded, autosetting the components to the manager on Start with the following functions:

 //Set PlayerStatus to this instance
 public void SetStatus (PlayerStatus status) { if(Status == null) Status = status;}
 
 //Set PlayerWeapons to this instance
 public void SetWeapons (PlayerWeapons weapons) { if(Weapons == null) Weapons = weapons;}

 //Set PlayerController to this instance
 public void SetController (PlayerController controller) { if(Controller == null) Controller = controller;}
 
 //Set UIPlayerManager to this instance
 public void SetUI (UIPlayerManager ui) { if(UI == null) UI = ui;}

I did all this mess because I had problems changing from one scene to another having the references still in the manager, because the Player beign not a DontDestroyOnLoad would make the manager loose all the references. But now I'm having doubts this is the worst method to use for such things, I would still be able to access the player references this way, because is easy and fast once is all loaded, but on more complex scenes may mess up the priority of loading. I know I can set script loading priority but I would like to hear for alternatives before messing with it.

Comment
Add comment
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
1
Best Answer

Answer by whydoidoit · Jun 15, 2013 at 10:36 AM

Well I'd do it in Awake only and make sure that the script execution order for your singleton is very early in the process or make sure that nothing accesses the values until their own Start function. An even better way is to avoid all that GameObject.Find stuff and just assign them in the inspector - always seems much more flexible to me, but that's horses for courses.

The benefit of that is that you can create an Instance property that removes the issues of Awake on other scripts needing access to the singleton's properties (you can also do this by forcibly calling the initialization function to be fair).

   static SomeSingleton _instance;

   public static SomeSingleton instance 
   {
         get
         {
            if(_instance == null)
               _instance = GameObject.Find("SingletonWellKnownName").GetComponent<SomeSingleton>();
            return _instance;
         }
   }

   void Awake() 
   {
       _instance = this;
   }
Comment
Add comment · Show 1 · 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
avatar image $$anonymous$$ · Jun 15, 2013 at 08:12 PM 0
Share

In the end I opted to make sure nothing about the singleton get executed until is everything setted up, so I set up each component from the components themself and then did a check like you suggested, is fast and less insanity inducing, so thank you for the suggestion.

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

14 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

Related Questions

How do I fix this error when trying to use the Watson SDK for Speech to Text? 0 Answers

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Access List from singleton database script 0 Answers

how to reference a javascript variables in a c# code. 0 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