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 /
avatar image
0
Question by 4illeen · Jul 19, 2011 at 08:59 AM · collisiontriggertriggerscollisions

Multiple triggers/collisions

Game objects like trees or rocks have this script attached

 using UnityEngine;
 using System.Collections;
 
 public class ColScript : MonoBehaviour {
     public void OnTriggerStay(Collider other) {
         if (other.gameObject.tag == "Cube")
         {
             Build._areaOverlay = true;
         }
     }
     public void OnTriggerExit(Collider other) {
         if (other.gameObject.tag == "Cube")
         {
             Build._areaOverlay = false;
         }
     }
 }

_areaOverlay is a public static bool found in Build class

Whenever _areaOverlay is true, the Cube which penetrates game objects with the script attached turns red - if it's equal to false the cube is green.

The thing is, when I'm inside at least two objects - let's say there's a tree and rock next to each other and Cube collides with both at the same time, when I leave the tree but am still inside the rock, Cube changes color for a second (color change is just a cosmetic, there are many game changes depending on _areaOverlay).

How do I avoid this? In other words, how do I check if Cube collides with other scripted game objects as well?

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 KeithK · Jul 19, 2011 at 09:08 AM

You could keep a count of how many objects Cube is colliding with. Either a list of object references, or a simple int.

Then you would only set Build._areaOverlay = false when your list is empty, or your count is zero.

You may also look at doing the collision checks on the Cube if possible? Since, it, is what is affected when there is a collision.

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 4illeen · Jul 19, 2011 at 09:26 AM 0
Share

I changed the script so it's now attached to Cube and reacts on game objects with tags 'tree' 'rock' etc. This allowed me to make a counter inside the script. Thanks a lot!

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Ignore collisions with self but still trigger with self 2 Answers

changing gravity OnTriggerEnter 1 Answer

2D Triggers and Collisions not working as expected. 2 Answers

Child Object's collider setting off triggers 1 Answer

Trying to make object turn red OnTriggerEnter 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