Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 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 schwertfisch · Jan 09, 2011 at 04:02 PM · colliderspawnemptycontain

How to know if a collider has a rigid body inside it or not?

In the board game (chess-like) that Im making, on the board, there will be my main gameObject that I control and various collectibles that my main object "swallows".

Each time it swallows a collectible, a new collectible will be spawned in a random position. This random position must be one of the "unoccupied" board boxes (those that don't have a collectible or my main object on them). So, I want to know every moment which squares of the board are occupied and which aren't.

I put an empty game object prefab on each of the board squares. This prefab has a Rigid Body component and a Box Collider attached. Depending on if another rigid body is inside this collider or not, the prefabs tag will change between 2 tags: empty/ full. What is the best way to do this? (control the tag's String)

I am thinking that maybe I could do this somehow using an OnTriggerStay function or a Selection.Contains class function? I dont have experience in using either one of them.

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

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

Answer by Matthew A · Jan 09, 2011 at 04:15 PM

I would suggest something like this:

var currentObjectInstance : int = -1;

function OnTriggerEnter(other : Collider) { if (other.gameObject.tag == "Thingy") { this.gameObject.tag = "Full"; currentObjectInstance = other.gameObject.GetInstanceID(); } }

function OnTriggerExit(other : Collider) { if (other.gameObject.tag == "Thingy" && other.gameObject.GetInstanceID() == currentObjectInstance) { this.gameObject.tag = "Empty"; currentObjectInstance = -1; } }

Edit: Adjusted the example. The tag is used so that we know it's the right kind of object (you might have projectiles / particles or whatever that could result in the OnTrigger functions begin called - you can skip the check if not). The instance id is used to make sure it's the same object that's entering / exiting the box (again, if only one object of that tag exists or can be entering / exiting at a time, you could skip the check).

Comment
Add comment · Show 3 · 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 schwertfisch · Jan 09, 2011 at 04:29 PM 0
Share

Thanks for answering $$anonymous$$atthew and for taking time to be so specific. So a Thingy tagged object enters the collider: (boardBoxTag=Empty-->OnTriggerEnter-->thingyPresent=true-->boardBoxTag=Full). I am using swipes to move my main object. After each swipe, I was planning on using GameObject.FindWithTag to create an array of objects with tag= Empty. In other words, this array will contain all the empty (at the end of my swipe event) positions on the game board. Then instantiate a collectible in one (random) of these empty board squares. Would I still need use "InstanceID"?

avatar image Matthew A · Jan 09, 2011 at 04:48 PM 0
Share

That sounds reasonable. I've changed the code example to reflect better what you said. Really what checks you perform depend on what objects you have that might set off the trigger (e.g. you might not want a Player object to set the box to full, but a collectible should).

avatar image schwertfisch · Jan 09, 2011 at 04:52 PM 0
Share

Thank you so much $$anonymous$$atthew, I really appreciate your help! Have a nice evening :)

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

No one has followed this question yet.

Related Questions

Simple Colliders Question 1 Answer

Spawn item at different location 2 Answers

Spawner continuing after boss dies 1 Answer

Help! I am working on a small project that I am having issues with on restarting the level on collision with another object in the scene. 0 Answers

How can I use raycasthit and collider raycast to hit only if the mouse position is over a specific object ? 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