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?
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?
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.
#pragma strict
public var infoHolder : Collision;
function OnCollisionEnter( col : Collision )
{
Debug.Log("On Object "+ col.gameObject.name);
infoHolder = col;
}
#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);
}
}
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.
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.