- Home /
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...
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.
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. :)
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.
Then could you accept the answer? Helps things stay clean... Edit: seems you accepted my answer ins$$anonymous$$d.
I don't care about the karma points, just confused how my answer wasn't exactly what you were looking for.
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.
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.
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.
Answer by tauqeerahmed · Aug 26, 2015 at 06:23 PM
No you can not add multiple RequieComponent with OR notation
Your answer
Follow this Question
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