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
1
Question by hamstar · Oct 08, 2013 at 10:22 AM · c#crashrequirecomponent

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?

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

2 Replies

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

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

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 hamstar · Oct 08, 2013 at 10:57 AM 0
Share

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.

avatar image
1

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.

Comment
Add comment · 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

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

17 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

App crashes after splash screen with Google Play Services for AR update (after April 11th 2020) 2 Answers

Inheritance vs RequireComponent -1 Answers

How do i access a SphereCollider added by RequireComponent? 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