Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 n3wb13 · Oct 14, 2017 at 12:49 PM · collider2drequirecomponent

How to set the property of my required component?

I want to specify that my CircleCollider2D is a trigger

 [RequireComponent(typeof(CircleCollider2D), ????? )]


or in this case I want my BoxCollider2D to be used by Effector

 [RequireComponent(typeof(BoxCollider2D), ????? )]
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
1
Best Answer

Answer by SoulRider · Oct 14, 2017 at 01:35 PM

 [RequireComponent(typeof(CircleCollider2D))]
 public class Foo : MonoBehaviour
 {
     private CircleCollider2D circleCol2D;
 
     private void Awake()
     {
         circleCol2D = GetComponent<CircleCollider2D>().isTrigger = true;
     }
 }

Comment
Add comment · Show 4 · 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 n3wb13 · Oct 14, 2017 at 02:10 PM 0
Share

No. I want the property to be triggered the moment I add the script not like this. RequireComponent(Type requiredComponent, Property requiredProperty, Property requiredProperty) something like that.

avatar image SoulRider n3wb13 · Oct 14, 2017 at 04:49 PM 1
Share

Unfortunately, you can't do it the way you would like, you need to do it the way shown (or indeed the shorter method by unit_nick which I edited in above).

avatar image Glurth SoulRider · Oct 14, 2017 at 05:36 PM 0
Share

calling circleCol2D.OnTriggerEnter(collidingCollider) manually, during awake won't work? (assu$$anonymous$$g one knows what collider to collide with)

avatar image unit_nick · Oct 14, 2017 at 02:11 PM 0
Share

which could even be simplified to

 GetComponent<CircleCollider2D>().isTrigger = true;
avatar image
4

Answer by Bunny83 · Oct 14, 2017 at 07:20 PM

RequireComponent is just a neat editor tool to automatically add a component to a gameobject when this script is attached through the editor. Note that this won't actually work when you add your script though code. It's a pure editor feature. It's not possible to specify initialization values in an attribute like this.

However what you want to use instead is implement the "Reset" method. Reset is only called once when the component is attached to the object or when you right click the component and select "Reset". It's ment exactly for that kind of initialization of your component at edit time.

 void Reset()
 {
     GetComponent<CircleCollider2D>().isTrigger = true;
 }

Though it would even be recommended to do the component check directly inside Reset. RequireComponent is not really reliable.

 void Reset()
 {
     var col = GetComponent<CircleCollider2D>();
     if (col == null)
         col = gameObject.AddComponent<CircleCollider2D>();
     col.isTrigger = true;
 }

This way you don't need the RequireComponent at all.

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

76 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 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 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 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

Add two components of the same type using RequireComponent attribute 1 Answer

[2D] Bug ground collision 1 Answer

Collider2D as 1 pixel size 2 Answers

collider2d how to measure?? 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