# Alternate Answer for collision problem?

I am building a top-down game in unity which works well. In this game, I have a 3D object (red cube) which a player has to control and there are other cubes like white, yellow and black cubes which spawns in the interval of 5 seconds. In each 5 seconds, prefabs of these three villain cubes instantiates and follows the player.

There is also a text that says yellow, black and white and changes constantly. For instance, the text is now white and it changes to black within certain interval of time and changes to white in certain interval of time (this is random) So, the player has to collide with the cube same as the the text otherwise the player will die.

Now, the problem is I have built the game just fine and can't get to deactivate player when the player hits the unrelated cube.

The script is(for instantiating:-)

public class ColorChecker : MonoBehaviour {
public GameObject[] text;
public int hold;

public float startTime;
public float startTimebtwSpawn;
// Use this for initialization
void Start () {

}

// Update is called once per frame
void Update () {
startTimebtwSpawn -= Time.deltaTime;
if (startTimebtwSpawn <= 0) {
startTimebtwSpawn = startTime;
hold = Random.Range (1, 4);
}
if (hold == 1) {
text [0].SetActive (true);
text [1].SetActive (false);
text [2].SetActive (false);

} else if (hold == 2) {
text [1].SetActive (true);
text [0].SetActive (false);
text [2].SetActive (false);

} else if (hold == 3) {
text [2].SetActive (true);
text [0].SetActive (false);
text [1].SetActive (false);
}
}
}


The script for destroying the object is:-

public class DestroyCubes : MonoBehaviour {
public ColorChecker colorchecker;
// Use this for initialization
void Start () {

}

void OnCollisionEnter (Collision col){
if (colorchecker.hold == 1 && col.gameObject.tag == "CubicMonster1") {
Destroy (col.gameObject);
} else if (colorchecker.hold == 2 && col.gameObject.tag == "CubicMonster2") {

Destroy (col.gameObject);
} else if (col.gameObject.tag == "CubicMonster3" && colorchecker.hold == 3) {
Destroy (col.gameObject);
}

}
}


Now, I can obviously check if it is unrelated by a long code like

if (colorchecker.hold == 1 && col.gameObject.tag == "CubicMonster2" && so on){

Destroy (gameObject);

}


Rather than writing a long if statement like this, Is there a better way that I can handle this ?

• Can you debug if its coming in your OnCollisionEnter if conditions? if you want to deactivate your Player so you can check only Player tag, you dont have to write long statement. – Saad Anees Jun 15 at 10:27
• I don't get if something is not working or you just want to avoid the long if statement for destroying the player? – Nikaas Jun 15 at 12:17
• @Nikaas Everything is working. I just want to avoid long if statement and that is all. – Sabin Pyakurel Jun 16 at 1:13

You can create a component like the next one:

public class EnemyType: MonoBehaviour
{
public int TypeOfEnemy;
}


Of course it can be made far better. For example instead of "int TypeOfEnemy" it can be an enum or even a list of enums. The whole idea is to avoid the poor Unity tags and make your own (multi)tag component and to be able to compare it with the numbers you use with ColorChecker.hold. But we will leave that beautifying for a homework :)

Anyway, assign that component to your enemies.

Then in your DestroyCubes component you can simply:

void OnCollisionEnter (Collision col)
{
if (colorchecker.hold == col.gameObject.GetComponent<EnemyType>().TypeOfEnemy)
{
Destroy(col.gameObject);
}
}


BTW I would instead move(and rework) the DestroyCubes from being part of the player to be a component on the colored cubes. Every game entity better be responsible for its own stuff and not one entity controlling the other. It's far easier to manage when things start to grow.

Finally I would have a code on a player component that "kills" the player:

if (colorchecker.hold != col.gameObject.GetComponent<EnemyType>().TypeOfEnemy)
{
Destroy(gameObject);
}


Note: I have not tested the code and it may contain some error.

P.S. It is good idea in general to avoid having hardcoded strings in your code, i.e. not have string literals like "some text". Imagine you decide to rename some of your tags, then your code will suddenly stop working correctly and you would never know that because there would be no error displayed. One day you will just notice your cubes no longer disappearing and you would have no idea why.