• 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 winkan · Aug 03, 2014 at 01:42 PM · collision

Detect collision from a script that not attached to object

I've a GameManager and I want to detect collision from that script, not from object. I want to do it like this:

public GameObject go1_;

void OnCollisionEnter(Collision col)

if(go1_.col.tag == "SomeTag"){ /// }

Is this not possible?

Comment
Add comment · Show 1
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 lgarczyn · Dec 03, 2019 at 02:04 AM 0
Share

Nope. Having a single Game$$anonymous$$anager handle all collisions is bad practice anyway.

What you can do is have the script call a function on your Game$$anonymous$$anager, possibly using a public UnityEvent.

3 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Aug 03, 2014 at 01:44 PM

Not possible. The OnCollisionEnter() must be on the object with the collider that will detect the collision. You can have that object inform your game manager.

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 winkan · Aug 03, 2014 at 01:50 PM 0
Share

Thanks mate.

avatar image
0

Answer by Mmmpies · Aug 03, 2014 at 07:14 PM

You couldn't do it with OnCollisionEnter but, as long as you only want a sphere type collider, you could test the distance between the object and whatever it is you want to trigger the collision.

If the distance is less than X then consider it collided with.

set the go1_ to be your player character for example then in update check the following

if(Vector3.Distance(transform.position, go1_.transform.position) < setDistance) { Do your code in here }

You wouldn't really want to do this with something that spawns lots of times though.

This is mainly handy if you have an object that needs more than 1 collider, I've used it for arrows. So the arrow collider allows it to stick into what it hits but if you get within 3 meters of it this script allows you to auto pickup the arrow.

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 winkan · Aug 03, 2014 at 07:23 PM 0
Share

Thanks mate, I did as "robertbu" said. There are 3 more scripts but it's what I need. Thanks.

avatar image
2

Answer by meat5000 · Aug 03, 2014 at 07:44 PM

You can do this but it requires pre-setup and it's Hacktastic.

In a nutshell. you define a class variable and use this variable in your OnCollision function to store the information created inside the OnCollision function. The trouble normally arises as the Collision info only has scope within the function, so when the function is no longer used the info goes with it. Simply store the information in another variable and it won't be destroyed. You can access this information in exactly the same way as any other variable.

Here I've knocked up a couple of scripts to show you how to go about this.

CollidingObject, goes onto the object which is involved in the collision. I called my object CollidingCube.

CollisionFind goes on to any object not involved in the collision.


//CollidingObject.js

 #pragma strict
         
 public var infoHolder : Collision;
         
 function OnCollisionEnter( col : Collision )
 {
      Debug.Log("On Object "+ col.gameObject.name);
      infoHolder = col;
 }


//CollisionFind.js

 #pragma strict
 
 private var colTest : Collision;
 private var colOb : GameObject;
 
 function Start ()
 {
     colOb = GameObject.Find("CollidingCube");
 }
 
 function Update ()
 {
     colTest = colOb.GetComponent(CollidingObject).infoHolder;
     if(colTest!=null)
         Debug.Log(colOb.gameObject.name);
     else
         Debug.Log("NULL COLLISION");
 }


Ok course this basic example is specific to one object, but I wanted to keep it basic.

You can use the principles to create a Collision manager of sorts.

The real scripting comes in when you consider different ways to access the gameObjects involved in the collisions, without knowing exactly which ones they are.


Note, @robertbu included this, simplified, in his answer

You can have that object inform your game manager.

as he is correct, you can not directly access the function from another object; only indirectly.

C# Edit: A little addition here, you can have your OnCollision routine fire a Function from another script and use the ref Keyword to pass the Reference to the actual collision that occured in OnCollision routine. The alternative in JS requires implementing this in a new class as out and ref keywords do not exist.

This does not allow you to re-use the actual Collision Physics but does allow you to use the actual Collision data generated and not a copy, which can lead to NullRef errors given that references can be lost.

 using UnityEngine;
 using System.Collections;
 
 public class CollisionPassing : MonoBehaviour {
     
     public GameObject recGO;
     public void OnCollisionEnter(Collision col)
     {
         recGO.GetComponent<CollisionReceiving>().ReceiveCollision(ref col);
     }
 }

 using UnityEngine;
 using System.Collections;
 
 public class CollisionReceiving : MonoBehaviour {
     
     public void ReceiveCollision(ref Collision col)
     {
         Debug.Log("Received :" + col.gameObject.name);
         OnCollisionEnter(col);
     }
     
     public void OnCollisionEnter(Collision col)
     {
         Debug.Log("MyCollision :" + col.gameObject.name);
     }
 }

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

6 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Unity2D side collision detection 1 Answer

How do I increase speed after collision? 2 Answers

Grab/Release object while pressing a key 1 Answer

Collision of a constantly moving object 0 Answers

Ignore Character Controller Collision 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