Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 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.

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

Asset Server and Dependencies 0 Answers

How do I include a shared asset in each bundle that uses it? (avoiding dependencies) 0 Answers

Import Cinemachine Plugin to an offline project 0 Answers

Network Event Timing 0 Answers

What is the order of execution for the instantiated object's GetComponent.Script.DoSomething? 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