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 GoMeteoroGo · Jan 31, 2014 at 10:54 PM · collidercomponentexceptions

"Missing Collider2D"

I'm having some trouble with a 2D box collider. right now, I've pared it down to this very simple code:

 void Update () {
         Debug.Log (this.collider2D.collider.bounds.center);
 }

When I try to run this, I get the following error:

"MissingComponentException: There is no 'Collider' attached to the "New Sprite game object, but a script is tryign to access it. You probably need to add a collider to the game object "New Sprite". Or your script needs to check of the component is attached before using it."

As of right now, "New Sprite" is the ONLY object I have, aside from the Main Camera. So, it's not as though I've accidentally put the collider on a parent or child of it. What's causing this error and how can I fix it so that I can make use of the object's collider? I can switch it out for a 3D box collider and the above code works just fine.

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
0
Best Answer

Answer by supernat · Jan 31, 2014 at 11:09 PM

You're actually trying to access a 3D Collider (just known as Collider) as a child of a 2D collider. I see why you did this, because you wanted the bounds, and you found it there. Unity provides a bounds object for Collider objects but not for Collider2D objects.

Can you just use the transform.position instead of collider center, or is there a specific need for this?

Since the 2D colliders are fairly new, I think this is a Unity bug, because it seems like 2D colliders should also provide a bounds object. But it's easy to work around:

     BoxCollider2D coll = GetComponent<BoxCollider2D> ();
     Vector2 cntr = coll.center;
Comment
Add comment · Show 2 · 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 supernat · Jan 31, 2014 at 11:10 PM 0
Share

Doh, took too long typing that up. rutter beat me to it. :) The one difference is that Collider2D doesn't provide a center, BoxCollider2D (if that's what you're using) does. Other specialized colliders may also offer the center member publicly.

avatar image GoMeteoroGo · Jan 31, 2014 at 11:20 PM 0
Share

Perfect! This is exactly what I'm looking for. Thanks!

avatar image
1

Answer by rutter · Jan 31, 2014 at 11:04 PM

Unity tries to make it easy to reference GameObjects and their components. Every component has shortcuts to find built-in components, such as renderers, rigidbodies, colliders, animations, particles, and more. Finally, there's the GetComponent function, which lets you find any component type (including your own).

You can think of these as roughly equivalent:

 this.collider2D //gets attached Collider2D component (or null)

 this.GetComponent<Collider2D>() //gets attached Collider2D component (or null)

Which means that this:

 this.collider2D.collider

Is a lot like doing this:

 Collider2D c2d = this.GetComponent<Collider2D>();
 collider c = c2d.GetComponent<Collider>();

It compiles, but there's almost no way that makes sense: even supposing you had both colliders attached, why wouldn't you just grab the one reference directly? I can imagine no reason to reference both types like that.

Collider and Collider2D do similar things, but they can't be used interchangeably. One works with 3D mechanics, the other works with 2D mechanics (which are new in Unity 4).

Most likely scenario here is that you've confused your collider types. It's important to keep straight whether you're handling 2D or 3D.

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 GoMeteoroGo · Jan 31, 2014 at 11:08 PM 0
Share

i'm definitely handling 2D. All I'm trying to do is reference the dimensions of the sprite's 2D box collider. Everything you just said makes perfect sense, but given all of that, what would be the way to go about doing that? With a 3D collider I can just use collider.bounds, but collider2D doesn't have a bounds class, and apparently collider2D.collider.bounds isn't valid either. So, what do I do?

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

20 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

Related Questions

Performance: Addcomponent BoxCollider in update method 0 Answers

how to tap on a .fbx file(including a group of objects) without adding 'box collider' component? 0 Answers

2D Animation does not start 1 Answer

Works after restart then crashes again ! "There is no 'Collider attached... a script is trying to access it" 0 Answers

Internal collisions 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