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
1
Question by Wolfdog · Apr 06, 2015 at 02:14 PM · unity 5scriptingproblem

Check collision from another object's script

Is ther a way to know what another object is colliding with? I have a bunch of child objects, each with a trigger collider. I want the parent to be able to know which children are colliding with an object. Here is my parent's script:

 foreach (GameObject child in transform) {
     if (child. /* on trigger stay includes an object with tag "taken", */){
         Debug.Log (child.transform.name + " is colliding with taken");
     }
 }

Is there a way to get a list of another object's collisions from script?

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

4 Replies

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

Answer by ethestel · Apr 06, 2015 at 03:48 PM

Hello, iterating through all childs on each physics update is not quite optimized. Try notifying parent only when a collision occurs. Try attaching this script to your parent object:

 using UnityEngine;
 
 public class ParentScript : MonoBehaviour {
 
     public void CollisionDetected(ChildScript childScript)
     {
         Debug.Log("child collided");
     }
 
 }

and this one to every child which are colliding

 using UnityEngine;
 
 public class ChildScript : MonoBehaviour {
 
     void OnCollisionEnter(Collision collision)
     {
         transform.parent.GetComponent<ParentScript>().CollisionDetected(this);
     }
 
 }
 

Please be aware that childs must be nested inside parents transform.

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 Wolfdog · Apr 06, 2015 at 05:16 PM 0
Share

I like this approach best.

avatar image Battles2000 · Nov 29, 2017 at 11:32 PM 0
Share

Hey what do you mean "nested inside"

avatar image
1

Answer by 237641 · Apr 06, 2015 at 03:26 PM

 //on child script
 void OnTriggerEnter()
 {
     collided = true;
 }
 //on parent script
 foreach(Gameobject child in transform)
 {
  if (child.collided)
     Debug.Log(child.transform.name + " collided with taken");
 }
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 Wolfdog · Apr 06, 2015 at 05:15 PM 0
Share

Thank you very much.

avatar image
0

Answer by meat5000 · May 09, 2016 at 01:35 PM

You can use ref keyword in C# or use GetComponent to pass the reference to the data.

http://answers.unity3d.com/questions/762798/detect-collision-from-a-script-that-not-attached-t.html#answer-763059

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
0

Answer by RandomGuy403 · Aug 15, 2021 at 11:46 PM

If your child is buried deep within your objects, you can use this instead. As you may have notice, the parent registers their method via delegate to each child, while the child just triggers the said delegate. if you're dealing with multiple colliders, you can also pass multiple children to your parent.

PARENT CLASS

 public class CollisionParent : MonoBehaviour
 {
     [SerializeField]
     public List<CollisionChild> colliders;
 
     // Start is called before the first frame update
     void Start()
     {
         foreach(CollisionChild child in colliders)
         {
             child.collide = onCollide;
         }
     }
 
     void onCollide(Collider collider)
     {
         Debug.Log("Collided");   
     }
 }

CHILD CLASS

 public class CollisionChild : MonoBehaviour
 {
 
     public delegate void Collide(Collider collide);
 
     public Collide collide;
 
     private void OnTriggerEnter(Collider other)
     {
         collide(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

23 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

Related Questions

How can I made a In app Purchase code (google play store) for unity 2019.1.9 ? 0 Answers

where have i went wrong in my script (unity 5) 0 Answers

How To Code Interior/Exterior Cell/Worldspaces Like Fallout/Skyrim 1 Answer

Script sets same value to other script in all objects instead of just one. 1 Answer

Refresh panel with prefab contained value from json that created using array 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