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
8
Question by axCed · Aug 01, 2013 at 01:44 PM · inheritancerequirecomponent

Is it possible to use || (or) with RequireComponent?

I made some scripts for my game objects that require a MeshCollider. [RequireComponent (typeof (MeshCollider))] is working fine. The thing is that some objects need different type of collider (box, sphere, etc) and therefore don't need a MeshCollider.

So, can I use RequireComponent with || statement to use only the needed component. I was looking for something like this: [RequireComponent (typeof (MeshCollider)) || (typeof(BoxCollider))].

I feel that it is impossible considering that Unity creates the required component for us at object creation...

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

5 Replies

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

Answer by gregzo · Aug 01, 2013 at 01:57 PM

Not possible out of the hat, no.

You could write an editor script for it, though. As soon as the script is instantiated in the hierarchy, it could open a popup asking you which of 2 components you want.

Excellent tutorial on editor scripts here.

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 axCed · Aug 01, 2013 at 02:31 PM 0
Share

Nice idea! ...but considering choosing the component in a editor window, I think I'll just remove the RequireComponent line and add the needed component manually. It's kind of the lazy way... :(

I think I'll try the popup. It might save some time in the long term. :)

avatar image
28

Answer by rhys_vdw · Aug 01, 2013 at 02:15 PM

RequireComponent requires a Type argument, and || returns a boolean, so || is not appropriate here. However you can have up to three arguments separated with commas.

 [RequireComponent( typeof(MeshCollider), typeof(BoxCollider) )]

If you need more than three, you can stack them up by having multiple RequireComponent directives above your class definition.

I'd avoid doing this too much though; it can get pretty annoying when you need to remove components. I usually just log a warning in the Awake function if the component isn't there.

Comment
Add comment · Show 7 · 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 axCed · Aug 01, 2013 at 02:26 PM 0
Share

Best explanation. :)

avatar image gregzo · Aug 01, 2013 at 02:32 PM 1
Share

Then could you accept the answer? Helps things stay clean... Edit: seems you accepted my answer ins$$anonymous$$d.

avatar image rhys_vdw · Aug 02, 2013 at 01:47 AM 0
Share

What a burn!

avatar image rhys_vdw · Aug 02, 2013 at 09:16 PM 1
Share

I don't care about the karma points, just confused how my answer wasn't exactly what you were looking for.

avatar image rhys_vdw · Aug 03, 2013 at 12:38 AM 1
Share

Well, if that's your preferred answer I can't argue with it. But to be clear, the RequireComponent with multiple arguments will attach any Components automatically, it's just a different syntax to what you expected.

Show more comments
avatar image
10

Answer by BMayne · Jan 11, 2015 at 06:43 PM

Hey there,

As people were saying you can write your own Inspector or even better an Attribute but that requires a bit of setup. What you could do is just use the Reset function that is in Unity on every MonoBehaviour.

   //Since we use editor calls we omit this function on build time
   [System.Diagnostics.Conditional("UNITY_EDITOR")]
   public void Reset()
   {
     AudioSource source = GetComponent<AudioSource>();
     Light light = GetComponent<Light>();
 
     if( source == null && light == null )
     {
       if( UnityEditor.EditorUtility.DisplayDialog( "Choose a Component", "You are missing one of the required componets. Please choose one to add", "AudioSource", "Light" ) )
       {
         gameObject.AddComponent<AudioSource>();
       }
       else
       {
         gameObject.AddComponent<Light>();
       }
     }
   }

Reset gets called when add a component so this would do what you want it to. Here is what it looks like when you are missing the component.

alt text


choice.png (7.1 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 apantev · Oct 26, 2015 at 07:54 PM 0
Share

This is perfect thanks!

avatar image
3

Answer by Buzzrick · Feb 08, 2017 at 10:47 PM

The correct solution here is to require the parent class for the objects that you need. For your example you'd need to require a Collider component, which is the base class for both BoxCollider and MeshCollider.

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
avatar image
0

Answer by tauqeerahmed · Aug 26, 2015 at 06:23 PM

No you can not add multiple RequieComponent with OR notation

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

23 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

"Injecting" an interface into an object 1 Answer

Polymorphism question for my custom class 1 Answer

Public variables from another class's nested class 1 Answer

Can someone help me fix my Javascript for Flickering Light? 6 Answers


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