I have been adding some enemies to a little project that I have been creating purely a learning exercise and try as I might I cannot get the collision event - OnCollisionEnter2D() - to register at all and I cannot understand why it is not occurring. The Prefab object that I am using has a 2D rigidbody and 2D collider attached. Now one of the colliding object is hitting the prefab (not kinematic) and bouncing of it but not registering a collision - the other is a missile/bullet thingy - using a trigger mechanism but also not registering a collision.
Any help is really, really, really appreciated. I have been at this for a while now trying different permutations of the script and cannot figure it out.
The code I am used is below, sorry about the length :(, I didn’t want to chop it up in case I missed out any relevant details:
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class EnemyController : MonoBehaviour {
// Use this for initialization
public static int numEnemies;
//access the prefab
public GameObject enemyPrefab;
Vector3 randomPos;
//need this to find position of enemy?!?
private Transform enemyPos;
/// <summary>
/// Start 'this' instance.
/// </summary>
void Start () {
print ("We are in Enemy Controller");
//print ("The Bat postion: " + myBat.transform.position.x);
numEnemies = 0;
}
/// <summary>
/// Update is Empty at this time
/// </summary>
// Update is called once per frame
void Update () {
}
public void SpawnEnemy(){
if (numEnemies < 3)
{
numEnemies++;
print ("Num Enemies: " + numEnemies);
RandomPosition ();
GameObject clone= (GameObject)Instantiate (enemyPrefab, randomPos, Quaternion.identity);
}
}
public void RandomPosition(){
randomPos.x = Random.Range (1f, 15f);
randomPos.y = 11;
randomPos.z = 0;
}
public void OnCollisionEnter2D(Collision2D col){
print ("Collsion Detected..");
}
public void OnTriggerEnter2D(Collider2D col){
print ("Collsion Detected..");
}
}//--> End EnemyController Class
I can only presume that this script is on the same GameObject that has the Collider2D or Rigidbody2D on it (that is colliding/triggering) otherwise you’ll not see a callback?
You’ll obviously only get OnCollisionEnter2D called if neither of the colliders are set to be a trigger. Likewise you’ll only get OnTriggerEnter2D called if either of the colliders are set to be a trigger.
Whoa! Hang on the the 2D Colliders/Rigidbody are on a prefab - so are you saying that this setup won’t work … like ever?
This has me confused! I want to be able to destroy the enemy 2 different ways, one with a ball and the other with a laser blast - guess what game I am trying to clone in the project :p. So it would need to have to be able to respond to both sets of collisions, boundary intercept and trigger intercept. So are you saying that this won’t either?
A prefab is an asset and does nothing as an asset. You must create an instance of it in the scene before it does anything. It’s this instance I’'m obviously referring to.
It sounds like you’re very new to all this as my explanation is pretty basic. Perhaps you should watch the 2D physics tutorial here? But to reiterate; when two colliders touch then you’ll get a OnTriggerEnter2D if either collider is a trigger or you’ll get a OnCollisionEnter2D if neither collider is a trigger. So a trigger collider always produces a OnTriggerEnter2D callback only.
Well obviously I am new, otherwise, I wouldn’t be asking a question like this! So, to way my way of thinking the SpawnEnemy() function is creating an instance of prefab in the scene. If it weren’t then why would be ball object be reacting to it and bouncing of it? The line of code that “seems” to be producing an object is:
Now this prefab has the rigidbody and colliders on it. The LevelManager manager calls the script by this functionality.
enemy = GameObject.GetObjectof Type();
then later in the Update function of LevelManager: if a certain condition is met then I call the Spawner function in EnemyController, ala:
enemy.EnemySpawner();
So, is this then creating a instance of the Object with the Rigidbody and colliders in place? Or, if not then why? And furthermore why does why ball bounce off the Object if the colliders, rigidbody are not in place as you are suggesting?
It is not obvious at all, you’d be amazed at how long some users have been using Unity and get confused over stuff that it’s assumed to be starter knowledge.
So rather than ask more questions, have you tried looking at the tutorials on either Physics or Prefabs?
Anyway, from your description now, it seems like you’re creating an instance of an ‘enemy’ prefab however when that ‘enemy’ instance collides with something (I have no idea what) then the callbacks will happen on that instance and not on the ‘OnCollisionEnter2D’ and ‘OnTriggerEnter2D’ on the ‘EnemyController’. Stick a script on your instance that has these callbacks instead.
Just because a script creates an instance of a GameObject doesn’t mean it’s somehow linked so there’s no reason why the creator script would get a callback.
No, I have not personally looked over those tutorials. I would like too, but, I am doing a course on Unity and between the course workload and the Martial Arts Instructing/training that I do, all my free-time is completely obliterated. Being the person that I am, I am rarely happy with the results of the course, and try to implement other features that have not been covered in the course … thus far.
D’oh! Now that makes complete sense and was the answer I was looking for! It also answers a number of other problems I was having in the project with regards to collisions! Just last night - actually at 4.00 am, I was thinking … I wonder if i need to take the “On” events and stick them on a file on the prefabs. I initially was doing that, but, someone told me that having “all” the code on the prefabs was bad practice. He was right of course, but, it led to this specific problem.
Thankyou for taking the time to answer my question it is greatly appreciated !
[EDIT] Adding the collisions to the prefab worked - collision and trigger .