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
1
Question by thesfid · Feb 04, 2014 at 07:23 PM · javascriptcollider

Why can't I check to see if a Collider component is null?

So I've got a number of objects in my scene tagged, and to simplify work in the editor I've written this code snippet (javascript) in function Awake().

 for (var collisionObj : GameObject in GameObject.FindGameObjectsWithTag("Collider"))
 {
 collisionObj.AddComponent("ShowContact"); // adds a script
 if (!collisionObj.BoxCollider) // check if it has a collider
      {
      collisionObj.AddComponent(BoxCollider); // if not, add one
      }
 }

Objects without colliders have them added without issue. Problem is, I keep getting the warning:

"Can't add component'BoxCollider' to abunchofstuff because such a component is already added to the game object!"

It doesn't seem to be affecting performance, but I get anxious when my console fills up like this. I've tried checking with both !BoxCollider as well as BoxCollider == null, and get the same result.

Am I misunderstanding how to check for an existing BoxCollider?

Any help is appreciated!

JMc

Unity Pro 3.41f4

Comment
Add comment · Show 3
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 Dblfstr · Feb 05, 2014 at 07:33 PM 0
Share

Did either of these answers work for you??

avatar image thesfid · Feb 05, 2014 at 10:11 PM 0
Share

Just saw the replies and I'm testing out now.

I had tried with just "Collider" as well as "BoxCollider" with the same result.

I'll feel really silly if it's just a matter of a lowercase letter.

avatar image thesfid · Feb 05, 2014 at 10:15 PM 0
Share

sigh It was just a matter of it being lowercase ins$$anonymous$$d of uppercase for the first letter.

Both answers were correct, marked the first as the accepted answer but game a +1 to both.

Thank you for helping!

2 Replies

· Add your reply
  • Sort: 
avatar image
1
Best Answer

Answer by Dblfstr · Feb 04, 2014 at 08:29 PM

collisionObj.collider = NULL if there is no collider attached.

Do these gameObjects tagged "Collider" have other colliders that you are concerned with? If not, just check if there is a collider, not necessarily a BoxCollider.

EDIT: I think this should work.

  for (var collisionObj : GameObject in GameObject.FindGameObjectsWithTag("Collider"))
         {
         collisionObj.AddComponent("ShowContact"); // adds a script
 
         if (collisionObj.collider) // check if it has a collider, null if no collider
         {
                Debug.Log("Collider attached to this GameObject");
         }
         else
         {
                collisionObj.AddComponent(BoxCollider); // if not, add one
         }
 
      }
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 thesfid · Feb 05, 2014 at 10:19 PM 0
Share

Thanks for the answer. To answer your question...

The scene is already quite full with content, and has been a collaboration in progress for quite some time. Some objects already have box colliders, and some do not. They're all box colliders, but just checking for collisionObj.collider works just fine.

avatar image
2

Answer by Lasent · Feb 04, 2014 at 08:31 PM

I'm coding in C#, so I might be completely wrong, but I don't think you could access the box collider of an object with .BoxCollider. Anytime, if you're referencing a component, the first is lower case, so even if this variable exists (according to the documentation, it doesn't), it should be boxCollider. One way to solve this, is if it's not important that it's a BoxCollider, to use:

 if (!collisionObj.BoxCollider)

If it's important that it's a BoxCollider, then:

 if (!collisionObj.collider || collisionObj.collider.GetType() != typeof(BoxCollider) // !collisionObj.collider comes first, so that there's no null reference error
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 thesfid · Feb 05, 2014 at 10:20 PM 0
Share

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

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

OnCollisionEnter2D not working? 2 Answers

Setting Scroll View Width GUILayout 1 Answer

Enable objects to enter the trigger 2 Answers

Go kinematic when in collider 1 Answer

Playing Different Animation Troubles 0 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