- Home /
How to Give OnCollisionEnter Priority for 2 prefabs ?
Hello there , I'm makin an Othello game and I'm having problem with the game pieces prefabs , I made black piece get destroyed when OnCollisionEnter with the white one , but my problem is how to make the white piece get destroyed when the black one spawn to in the same location ?
Is there is a way to give priority to them or so they meet the required conditions to be destroyed ?
This is the script on white pieces :
using UnityEngine; using System.Collections;
public class WhiteOnCollision : MonoBehaviour {
void Start () {
}
void OnCollisionEnter (Collision Stone){
if(Stone.gameObject.name == "Black Stone"){
Destroy(Stone.gameObject); }
}
void Update () {
} }
Note Please : I tried the same script on the black pieces with name=="White Stone" and the black one only get destroyed no matter what and there is no priority ..
If more information required for the question please tell me ,Thanks in advanced
Answer by taxvi · Dec 11, 2014 at 09:02 AM
you're almost there, just add this bit to your if statement:
if(Stone.gameObject.name == "Black Stone" && Stone.gameObject.name != gameObject.name){
Destroy(Stone.gameObject);
}
or, i think it's better to go more general, this script should work for both objects:
if(Stone.gameObject.name != gameObject.name){
Destroy(Stone.gameObject);
}
Thanks for replying But can you please explain what this do ?
I'm still a bit new to unity so a little explanation would help :)
I actually tried that code on both stones scripts and now they destroy each other :(
Your answer
Follow this Question
Related Questions
Distribute terrain in zones 3 Answers
Multiple Cars not working 1 Answer
How to choose what collider OnColliderEnter works with? 1 Answer
How do you trigger a specific collider using OnTriggerEnter? 2 Answers
How to detect collisions in C# 2 Answers