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 /
  • Help Room /
This question was closed Sep 01, 2017 at 06:24 PM by Geads for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by Geads · Sep 01, 2017 at 04:37 PM · scripting problemscript.boxcollider2dscriptingproblemscriptable object

Help with [SerializeField] in a script!

Hello everyone, I have a script that works, but my issue is that I have to put it multiple times and drag a box collider 2d to get it to work.

alt text

What I'm trying to achieve is to somehow be able to drag the box colliders to the same script so I don't have to have the same script multiple times with different colliders.

alt text Here is my script

 public class IgnoreCollision : MonoBehaviour {
 
 
     [SerializeField]
     private Collider2D other;
     // Use this for initialization
     private void Awake()
     {
         
         Physics2D.IgnoreCollision(GetComponent<Collider2D>(), other, true);
     }
     
 }

Thank you for the help!

script2.png (15.0 kB)
script1.png (15.6 kB)
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

1 Reply

  • Sort: 
avatar image
1
Best Answer

Answer by Vicarian · Sep 01, 2017 at 06:00 PM

Simply convert the type of the field to a collection, such as an array or a List:

 public class IgnoreCollision : MonoBehaviour {
     // Notice the array operator ([]) and the pluralization
     [SerializeField] Collider2D[] others;  
 
     void Awake() {
         foreach (Collider2D other in others)
             Physics2D.IgnoreCollision(GetComponent<Collider2D>(), other, true);
     }
 }

Since you're trying to do it this way, you can edit the Physics system to prevent collisions between layers. Edit > Project Settings > Physics and Physics2D. The triangle-shaped set of checkboxes allow collision for layers where the boxes intersect. Unchecking these disables collision between these layers. Something to consider, at least.

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 Geads · Sep 01, 2017 at 06:20 PM 0
Share

Wow, thank you so much, that made my life so much easier!

Follow this Question

Answers Answers and Comments

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

How can I get all cameras enabled true false states ? 0 Answers

Set public gameobject by raycast hit target 1 Answer

Problem with the cloning object :( 0 Answers

What am I doing wrong? 2 Answers

UnityCar - Controlling Speed with a duration? 2 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