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
4
Question by PaxForce · Jan 03, 2015 at 10:00 AM · ontriggerentergameobjects

how to detect OnTriggerEnter on ANOTHER gameObject

How to detect OnTriggerEnter on ANOTHER gameObject? I know how to detect such event on the object where the script itself is attached. In my mini-game I would like to use a script on my main object and from there detect a collision/triggerEnter on another gameObject. Is that even possible?

Comment
Add comment · Show 2
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 Intervention · Jan 03, 2015 at 10:11 AM 0
Share

I don't believe so. I think what I would do would be to attatch a script to the other game object and on startup get a reference to your main object's script. Then in that other object's ontriggerenter, just call a function on your main object's script. That way, your main object's script can get a notification when the other object's script detects a collision.

avatar image PaxForce · Jan 03, 2015 at 10:22 AM 0
Share

@Intervention I'm aware of that, in fact, that is how I have it set up at the moment. What bothers me is, that I have a lot of sub-objects that need collision detection and thus they have to have the same collision detection script attached to them. I wanted to have one script on my main object and iterate through the sub-objects when detecting collision. Not that I wouldn't trust you :) but I want to be absolutely sure - I'll wait for a couple of days for some more responses to my question and we'll see.

3 Replies

· Add your reply
  • Sort: 
avatar image
5

Answer by TimCoster · Mar 22, 2021 at 09:44 AM

Hey future me and other me's, you can use those unity events!

On the other GameObject:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.Events;
 
 public class OnTriggerStayEvent : MonoBehaviour
 {
     public UnityEvent<Collider> onTriggerEnter;
 
     void OnTriggerEnter(Collider col)
     {
         if(onTriggerEnter != null) onTriggerEnter.Invoke(col);
     }
 }


On this GameObject:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class Example : MonoBehaviour
 {    
     public OnTriggerEnterEvent onTriggerEnterEvent;
   
     void OnEnable()
     {
         onTriggerEnterEvent.onTriggerEnter.AddListener(OnTheOtherTriggerEnterMethod);
     }
 
     void OnDisable()
     {
         onTriggerEnterEvent.onTriggerEnter.RemoveListener(OnTheOtherTriggerEnterMethod);
     }
 
 
     void OnTheOtherTriggerEnterMethod(Collider col)
     {
         if(col.gameObject.tag == "Enemy")
             Debug.Log("Enemy entered the trigger!");
     }
 }

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 robwill · Dec 31, 2021 at 03:53 PM 1
Share

shouldn't your second code block use "onTriggerStayEvent" instead of "onTriggerEnterEvent" to match the first code block?

avatar image
4

Answer by tanoshimi · Jan 03, 2015 at 10:37 AM

You can't. OnTriggerEnter() messages are sent to the trigger collider and the rigidbody (or the collider if there is no rigidbody) that touches the trigger only - you can't subscribe to them from a 3rd party script. http://docs.unity3d.com/ScriptReference/MonoBehaviour.OnTriggerEnter.html

You can of course create a global collision manager, but each object will have to be responsible for updating it with their own collisions. Assuming all your sub-objects are created from the same prefab, or derived from the same parent class, you simply attach your collision script there.

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
1

Answer by Headrush95 · Apr 12 at 09:11 PM

for those who still need this you can do this.

on the parent (weapon holder) script you can create a method like this. the method will take a collider and then we can assign it to anything.

 public class WeaponsController : MonoBehaviour
 {

 //we would drag and drop the weapon in the inspector
 public GameObject weapon;

 private float Damage;
 private Collider EnemyCollider;  
 private bool enemyHit;

      public void Start()
      { 
            //on our weapon we may have a script called  "Sword" that contains all of the sword 
            //information, such as "Damage"

            Damage = weapon.GetComponent<Sword>().Damage;
      }

     public void EnemyHit(Collider other)
     {

             EnemyCollider = other;

             //if we have a health script on our enemy we may want to damage the enemy
             //so lets say we have a health script
             //we could call the take damage function on the enemy 
             //that was hit

             EnemyCollider.GameObject.GetComponent<Health>().TakeDamage(Damage);
             Debug.Log("Enemy Hit");
             enemyHit = true;

             // here we can take the collider that our weapon has sent us and we can assign
             // it to a new variable so that we can make any changes.

            
         }
     }

then on the weapon you can create a script and do this. you can check for collision and then call the function from your parent script and use the collider that was detected as the collider in the method you created.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class WeaponCollisionDetection : MonoBehaviour
 {
    public WeaponsController WC;

    public void OnTriggerEnter(Collider other) 
   {
 
     if (other.transform.tag == "Enemy")
     {
 
         WC.EnemyHit(other);
 
     }
 
   }
 }

this will allow you to grab what ever your weapon hit and then do what ever to it, such as damage it.

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

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

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

Related Questions

OnTriggerEnter() not firing for mutliple objects 0 Answers

how can I add a second ontriggerenter to my bullet script? 4 Answers

OnTriggerEnter() and OnTriggerExit() called multiple times despite checks. 3 Answers

How to make player go through object but can't go back 3 Answers

On trigger turn off particles and lights make room dark,On trigger by FPC disable/destroy lights and particles 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