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
2
Question by Jordi · Dec 29, 2010 at 09:40 PM · triggerontriggerenter

OnTriggerEnter through a different object

WHat I'm trying to do, is access one object's onTriggerEnter via the script on a second object. I've tried it as follows:

touchRange.OnTriggerEnter(Collider coll) {

     if (coll.gameObject.name == "Player") {
         playerCollision = true;
         itemScript.curTouchedItem = itemName;
         itemScript.amountOfItemsTouched++;

     }
 }

Where touchRange is an empty gameObject with a boxCollider (as trigger) that is a child of the object this script is attached to.

I hope it's not too vague ^^

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

2 Replies

· Add your reply
  • Sort: 
avatar image
3

Answer by Jessy · Dec 30, 2010 at 03:20 AM

You can't do what you want to do, directly. However, you can have OnTriggerEnter trigger an event, and have other classes listen for that event. This will yield better data encapsulation over what you are doing now.

Put this script on the game object with the collider:

public delegate void EventHandler (); public event EventHandler CollideWithPlayer;

// Checking a reference to a collider is better than using strings. [SerializeField] Collider playerCollider;

void OnTriggerEnter (Collider collider) { if (collider == playerCollider) CollideWithPlayer(); }

And on the parent object, you can use this:

bool playerCollision;
void Awake () {
    touchRange.CollideWithPlayer += () => playerCollision = true;       
}

itemScript.this and itemScript.that are not things you want to do. Instead, listen to the CollideWithPlayer event in ItemScript, directly, so things happening in ItemScript are more self-contained.

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 superme2012 · Aug 13, 2013 at 09:11 AM 1
Share

Yeah this is how I always do it to, but it is strange that there is no method to detect the trigger from a remote/abstract method.

There must be a way to do this as it would surely centralise code for triggers?

avatar image
0

Answer by Headrush95 · Apr 12 at 10:37 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);
  
      }
  
    }
  }
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

2 People are following this question.

avatar image avatar image

Related Questions

Rigidbody + Multiple Trigger Colliders - OnTriggerEnter - Get which Collider triggered the event issue 1 Answer

C# - Problem with trigger that won't activate 1 Answer

OnTriggerEnter/Exit called unexpectedly 2 Answers

Checking Tag of Trigger Prefab 1 Answer

Making OnTriggerEnter execute on startup 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