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 TifaOng · Jan 19, 2014 at 07:35 AM · c#collidertrigger

Trigger collider information

I have 1 object with a collider with isTrigger true. How to detect the number of objects inside the collider and assign each object a variable?

What I want to achieve is, if 2 objects are inside the collider, find the nearest object and do action. If only 1 object, just do the action.

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

3 Replies

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

Answer by moghes · Jan 20, 2014 at 09:24 AM

I will explain you more what the guys were trying to tell you, I will use tags. assign a tag to your objects that will enter the trigger. Here i have assigned "guest".

 using UnityEngine;
 using System.Collections;
  
 public class MyClass : MonoBehaviour 
 { 
     private int count = 0;
  
     void Update () 
     {
        if (colliders.Count == 1)        
          Debug.Log ("Do action based on 1");
        
        else if (colliders.Count == 2) 
          Debug.Log ("Find the nearest and do action for 2");       
     }
  
     void OnTriggerEnter(Collider col)
     {
        if(col.transform.tag == "guest")
             count++;
     }
  
     void OnTriggerExit(Collider col)
     {
        if(col.transform.tag == "guest")
             count--;
     }
 }
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 TifaOng · Jan 21, 2014 at 01:32 AM 0
Share

Okay I see, thanks! I will try this out :)

avatar image
2

Answer by ankush_Kushwaha · Jan 19, 2014 at 08:19 AM

you can track and keep the number or count of the objects present in your collider. You can declare a variable count and in your OnTriggerEnter() increment it by 1 and in your OnTriggerExit() decrement it by 1. So now the value of count will tell you the number of objects present inside the collider.

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 TifaOng · Jan 20, 2014 at 02:13 AM 0
Share

Hi, does OnTriggerEnter() still works when an object is already in the trigger, then another object enters, it will +1? How do I get the transform of the objects in the trigger? The parameter, Collider coll can only store 1 object triggered, but what about multiple objects?

avatar image
0

Answer by robertbu · Jan 19, 2014 at 08:04 AM

First, non-collider trigger solutions are easier but less accurate for this task. For example, Physics.OverlapSphere(). But there is a way to do it with a trigger.

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
  public class Bug2 : MonoBehaviour {
 
     private List<Collider> colliders = new List<Collider>();
 
     void Update () {
         if (colliders.Count == 1) {
             Debug.Log ("Do action based on 1");
         }
         else if (colliders.Count == 2) {
             Debug.Log ("Find the nearest and do action for 2");
         }
     }
 
     void OnTriggerStay(Collider col) {
         colliders.Add (col);
     }
 
     void LateUpdate () {
         colliders.Clear ();
     }
 }

At each frame, when you reach Update() 'colliders' will be a list of all the colliders found in the OnTriggerStay(). You can compare the two (or more) to see which is closest.

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 TifaOng · Jan 20, 2014 at 02:37 AM 0
Share

collider.Add is like in Array? you clear and add items? Thanks!

avatar image NGC6543 · Oct 29, 2015 at 07:20 AM 0
Share

Hey, Thanks for the nice tip! I tried your code, but sometimes the List.count returns 0, or sometimes the count is almost double the actual count.

for my game the 20 characters are actively go in and out of a trigger(quite randomly and frequently) and I think frame skipping is occuring..

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

21 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 avatar image

Related Questions

Triggering candle lights on with Colliders 1 Answer

Erratic trigger behavior, I can't find the fault. 1 Answer

Triggers Interacting with Triggers 0 Answers

I need help with triggers 1 Answer

Return gameobject from a TriggerEvent. 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