Collision detection problem between prefab and gameobject
I can't wrap my head around this problem. I've tried adding onCollisionEnter and onTriggerEnter on both my cube and my bullet prefab but neither of them detects a collision between them. I've searched on google on this problem but couldn't find a solution that worked.
This is my script on my cube gameobject.
using UnityEngine;
using System.Collections;
public class testcollision : MonoBehaviour {
public GameObject bullet;
void onTriggerEnter(Collider other) {
if(other.gameObject.tag == "bullet" ) {
Destroy(this.gameObject);
}
}
void update() {
}
}
This is the inspector menu on the cube gameobject: http://i.imgur.com/M0WDZIX.png
This is how my bullet is being made. It gets shot from my player character.
using UnityEngine;
using System.Collections;
public class AutoattackBULLET : MonoBehaviour {
public GameObject Bullet_Emitter;
public GameObject Bullet;
public float Bullet_Foward_Force;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(Input.GetKeyDown("a")) {
GameObject Temp_bullet_handler;
Temp_bullet_handler = Instantiate (Bullet, Bullet_Emitter.transform.position, Bullet_Emitter.transform.rotation) as GameObject;
Temp_bullet_handler.transform.Rotate(Vector3.left * 90);
Rigidbody Temp_Rigidbody;
Temp_Rigidbody = Temp_bullet_handler.GetComponent<Rigidbody>();
Temp_Rigidbody.AddForce(transform.right * Bullet_Foward_Force); //use transform.right
Destroy(Temp_bullet_handler, 0.4f);
}
}
}
This is the bullet prefab's inspector menu: http://i.imgur.com/sFiAlZU.png
The bullet does bounce off the cube but I can't get a collision check anywhere.
OnTriggerEnter will never be called, because none of your objects is a "Trigger", use OnCollisionEnter ins$$anonymous$$d. And, why you have the same tag "bullet" on both gameobjects ,i.e., Bullet prefab and cube?. Try changing the tag of Cube and see if it works.
Yeah, I tried those changes. I must have forgotten the tags when I was messing around with it. The problem persists. Nothing happens...
Answer by romatallinn · Nov 22, 2016 at 05:49 AM
It is important to have OnTriggerEnter, but not onTriggerEnter (capital O is needed).
If I use OntriggerEnter, what settings would I need to change?
Enable "Is Trigger" in the Collider component of 1 of the GameObject.
Thanks guys. I can't believe that I spent hours just because of a capital O....
Again, it is important to use correct spelling - OnTriggerEnter. :)
Your answer
Follow this Question
Related Questions
Detecting collisions between instantiated prefabs? 0 Answers
What is the 3D equivalent of using Physics2D.OverlapArea to check for if a character is grounded? 2 Answers
2D Platformer Character Acts Weird When Collide Side of a Platform 0 Answers
App works fine on PC but not on android 0 Answers
Raycast doesn't work properly 1 Answer