• 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 ivarhill · Dec 27, 2012 at 04:37 PM · bounds

Cannot get Bounds.Intersects to work

I have this if statement that only runs a specific function if two objects doesn't intersect:

 Bounds roomBounds = GetComponent<MeshFilter>().mesh.bounds;
 Bounds newRoomBounds = newRoom.GetComponent<MeshFilter>().mesh.bounds;
 if(possibleDirections.Count > 0 && roomBounds.Intersects(newRoomBounds) == false)
 {
 code to be run here
 }



I've also tried using MeshRenderer.renderer.bounds:

 Bounds roomBounds = GetComponent<MeshRenderer>().renderer.bounds;
 Bounds newRoomBounds = newRoom.GetComponent<MeshRenderer>().renderer.bounds;

However, when I launch my game in unity, the if statement makes it freeze. If my if statement simply is if(possibleDirections.Count>0), it runs fine. Any idea what I am doing wrong?

Comment
Add comment · Show 4
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 Dave-Carlile · Dec 27, 2012 at 04:48 PM 0
Share

What code do you have inside the if? I would add some Debug.Log messages to make sure value are what you think they are. Add one before the if to see what possibleDirections.Count is, and inside the if to see if it gets in there. Comment out all other code that's inside if to make sure that isn't causing a problem.

avatar image ivarhill · Dec 27, 2012 at 06:08 PM 0
Share

I've managed to rule out everything but the Bounds.Intersects part - if I comment out everything inside the if statement it still freezes, and I've tried Debug.Log() messages on all values involved - they seem to be correct.

avatar image Dave-Carlile · Dec 27, 2012 at 06:26 PM 1
Share

What do you mean by "freezes"?

avatar image Jessy · Dec 27, 2012 at 06:52 PM 0
Share

Exactly. Without seeing more, I'd say null roomBounds or newRoomBounds is null, if not both. possibleDirections is irrelevant to this question. Bounds.Intersects probably is too.

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Bunny83 · Dec 27, 2012 at 06:59 PM

Mesh.bounds won't help you here since it's in local space. You have to use renderer.bounds like in your second example. An if-statement can never make an application freeze. Like Dave said what's inside your if block? I guess the code in there might freeze your app.

BTW the MeshRenderer is already the renderer on this GameObject, so you don't need the GetComponent.

     Bounds roomBounds = renderer.bounds;
     Bounds newRoomBounds = newRoom.renderer.bounds;
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
avatar image
0

Answer by ivarhill · Dec 27, 2012 at 10:17 PM

Update: I've replaced Mesh.bounds with renderer.bounds. I've also done Debug.Log checks on both bounds (neither is null), and also a Debug.Log(roomBounds.Intersects(newRoomBounds)) - this correctly shows the result in the console.

The if block contains this single row:

 collides = true;

And commenting it out still makes it freeze.

So oddly enough, the command itself works, but whenever I put it in an if statement Unity freezes - as in, I press the Play button, it loads for a few seconds, then I cannot press anything and have to use the Task Manager to quit the program.

I have no idea what's going on here...

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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

11 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

Related Questions

Checking if object intersects? 1 Answer

Editor Script renderer.bounds.intersect does not work 0 Answers

Problem with checking for intersection between two rotated objects 3 Answers

Determine if two meshes overlap, without using bounds or trigger? 0 Answers

when should Mesh.Optimize() be called 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