Quantcast
Channel: Questions in topic: "class instance"
Viewing all articles
Browse latest Browse all 131

Control which class is instantiated first

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

Viewing all articles
Browse latest Browse all 131

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>