- Home /
OnCollisionEneter() does nothing C sharp
hey, im trying to make it so when an object its created anything colliding with it, incouding stuff inside if it are stored in a variable. anyway, i cant even make it so it shows a message when it collides. here is the script im using
using UnityEngine;
using System.Collections;
public class generatorScript : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnCollisionEnter (Collision collision) {
Debug.Log("Something is there");
if (collision.transform.tag =="Base"){
Debug.Log("structure:" + collision.transform.name);
Transform structureLink = collision.transform;
}
}
}
the part that i do not understand is that the objetct to wich this script is attacked is in contact with 3 other objects, anyway when i play the game absolutely nopthing happens. any help is much apreciated.
If you want to detect a collision: Both of the objects which are colliding must have colliders enabled, can´t have IsTrigger set to true and at least one of them must have rigidbody component which doesn´t have $$anonymous$$inematic option set to true. Do your objects fulfill these things?
yea, tho im using an oler version of unity cuz im to layzy to update. could it be a bug?
Are you sure that the objects you want to compare have a layer that allow them to collide?
I'm not sure exactly but OnCollisionEnter probably only registers those collisions after it moves after being created, or if it is created with objects inside it it probably won't check those until they're apart and then together again.
The best option is just to use Physics.OverlapSphere(), it does exactly what you're doing and you can run it in the start funfction.
Your answer
Follow this Question
Related Questions
Why is my OnCollisionEnter function working for enemies and not my player? 1 Answer
On Trigger Enter, Collide with object, specific collision 1 Answer
Collisions and Lists 1 Answer
Collision Only being detected on one of the objects involved in the collision - C# 0 Answers
Colliding two GameObjects 1 Answer