- Home /
Can two components require each other?
I want to make sure a Controller component always has access to a UI component. The UI component would never be used without the controller, so they require each other. However, Unity instantly closes/crashes when I try to add one of these components to a game object. There are no error messages.
I also tested this with two "empty" scripts, as below.
[RequireComponent(typeof(ScriptB))]
public class ScriptA : MonoBehaviour {
}
[RequireComponent(typeof(ScriptA))]
public class ScriptB : MonoBehaviour {
}
If there is a logical reason why I shouldn't be doing this could someone explain?
Answer by vexe · Oct 08, 2013 at 10:31 AM
I think what's happening is some sort of infinite requiring loop, where ScriptA
says: "I require ScriptB", well let's go to ScriptB
, which in turn says: "I require ScriptA", well let's go to ScriptA
, which says: "I require ScriptB", etc. This keeps on going until Unity crashes. Kinda like in C++ when you're #including A in B, and B in A. So here's your logical reason of not doing this.
Instead of that, you could let each script store a reference to the other script, and in your Awake/Start
your just add the component if there wasn't any:
public class ScriptA : MonoBehaviour
{
public ScriptB scriptB;
void Awake()
{
scriptB = GetComponent<ScriptB>();
if (scriptB == null)
scriptB = gameObject.AddComponent<ScriptB>();
}
}
And do the same for ScriptB
Thank you and @Hoeloe. I hadn't though of it that way. I had just assumed the component would be added before the requirement check took place.
Answer by Hoeloe · Oct 08, 2013 at 10:40 AM
I know exactly what's happening. When you add a script to an object, it searches for and required scripts, and attaches them to the object as well, in order to preserve dependencies. So, what is happening here is that, when you attach ScriptA to the object, it finds that ScriptB is required, and adds that. But then it sees that ScriptA is required, and adds that, which requires ScriptB... etc. Unity is crashing because it gets stuck in an infinite loop (I just tested this myself, and mind didn't crash, because it terminated the loop with a StackOverflowException). The issue is not so much with circular dependencies themselves, but with how Unity handles them. You can, however, get around this by removing the RequireComponent statement, adding the script to the object, then replacing the RequireComponent statement (in fact, I have had to do this for a prefab in one project, so I can select one of many inherited scripts to attach upon instantiation). This bypasses Unity's dependency check, and will allow only one script to be added at a time. Be warned that the console will continuously log errors during play if a scene object does not contain a required script.