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
0
Question by Aggressor · Feb 22, 2015 at 10:01 PM · requirecomponent

RequireComponent only blocks removal? Can I force a warning if missing at runtime?

Hi

I have several scripts I want associated with a GameObject and I don't want that GameObject to exist in the scene without these components.

As such I have attached a script to that GameObject which has a bunch of:

 [RequireComponent (typeof(ScriptName))]

However, if I run the game without those required components I don't even get an error. If I add the component and try to remove it, I get an error message and it wont let me. But, there is no safegaurd/warning if they required component is never added.

Is there a way I can FORCE an object to get scripts added to it if its missing from the required (ideally in the IDE and not in the code)?

Comment
Add comment · Show 2
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 steakpinball · Feb 23, 2015 at 08:31 PM 0
Share

RequireComponent is supposed to add the required components when you attach the script in edit mode. Is this not what you're experiencing?

avatar image Aggressor · Feb 23, 2015 at 08:41 PM 0
Share

Correct. It wont let me remove it when I add the component, but if I add a script with requires, those requires are not automatically added

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Nymisu · Feb 23, 2015 at 08:19 AM

Um...

 ScriptName mahscript;
 
 void Start();
 {
 mahscript = ***.getComponent<ScriptName>();
 if (mahscript == null) Debug.Error("Oh no!");
 }

Additionally, you can do this in editorscript too, if you'd like.

Comment
Add comment · Show 3 · 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 Aggressor · Feb 23, 2015 at 08:28 PM 0
Share

Yes, perhaps I was unclear, I was hoping to have these added when I drag the script on to the object, not at runtime.

avatar image Nymisu · Feb 24, 2015 at 11:50 AM 1
Share

In that case, use an editorscript that does that if GetComponent() returns null. Don't feel put off by the complexity of editorscripts, all you really need to is:

 //this goes in the "Editor" folder
 public class YourscriptsEditor : Editor {
 
 private yourscript x;
 private static bool addedthat = false; //optimization
 
 OnInspectorGUI() 
 {
   if (x == null) x = (yourscript)target;
   if(!addedthat)
   {
     if (x.getComponent<componentyouwant>() == null) x.gameObject.AddComponent<componentyouwant>();
     addedthat = true;
   }
 //and then draw as normal
   DrawDefaultInspector ();
 }
 
 }
avatar image Aggressor · Feb 26, 2015 at 04:01 AM 0
Share

Interesting point Ill take a look at that thank you!

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

21 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

Related Questions

Get types from UnityEngine.dll failed on RequireComponent 1 Answer

How to set the property of my required component? 2 Answers

How does RequireComponent expose properties 1 Answer

RequiredComponent with Custom Types 1 Answer

How to use RequireComponent with ParticleEmitters? 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