• 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 oxidedivision · Jan 22, 2019 at 06:36 PM · dependenciesclass instanceorder-of-execution

Control which class is instantiated first

In the scenario where I have two classes, AClass and BClass, which both have some basic code in the constructor, but AClass relies on BClass being instantiated first , how can I control the order of instantiation?


I guess I'm also looking to know how to control which scripts hit the Unity event functions (Awake(), Start() etc) first.


More details:


I have have two GameObjects in my scene (no parent/child relationship), and they each have their own class attached:


  • ObjectA has AClass

  • ObjectB has BClass


Bclass needs to be instatiated after AClass, because the code relies on this:


 public class AClass : MonoBehaviour
 {
     public static AClass ObjectReference;

     public AClass()
     {
         if( ObjectReference == null )
             ObjectReference = this;
         else
         {
             Destroy( ObjectReference );
             ObjectReference = this;
         }
     }
 }

 public class BClass : MonoBehaviour
 {
     bool Pass;

     public BClass()
     {
         if (AClass.ObjectReference != null )
         {
             Debug.Log( "AClass was instantiated first" );
             Pass = true; // we obviously want Pass to be true
         }
         else
         {
             Debug.Log( "BClass was instantiated first" );
             Pass = false;
         }
     }
 }
Comment
Add comment · Show 1
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 Bonfire-Boy · Jan 22, 2019 at 07:06 PM 1
Share

You shouldn't be declaring constructors for your $$anonymous$$onoBehaviours at all (if you want to know why, just search this site for "$$anonymous$$onobehaviour constructor" and you'll find plenty of discussion of the matter). Ins$$anonymous$$d, do your set up using the Start and Awake functions that get called automatically. If part of the set-up of one component is dependent on another component being set up first, then put it in a (different) initialisation function so that you are in control of when it happens, and call that function as part of (eg at the end of) the other class's set-up.

1 Reply

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

Answer by TreyH · Jan 22, 2019 at 08:04 PM

You're thinking of Script Execution Order. You can configure that in your Project Settings:

alt text


order.gif (342.9 kB)
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 Bonfire-Boy · Jan 23, 2019 at 08:41 AM 0
Share

@oxidedivision Good to see you've got this working but you really must stop writing constructors for your $$anonymous$$onoBehaviours. It'll only cause problems.

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

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

99 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

Related Questions

Select Dependencies on scene shows material not in use 0 Answers

Unable to remove old dll dependencies 1 Answer

Asset dependencies - selection/export 0 Answers

Toggle, script order causing issues 1 Answer

Call function after other objects have run their Start() function 2 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges