OnMouseDown - if statement

Hello.
I hope you can help me.
I am trying to disappear an object when I click another one.
I want to distinguish between two different objects.
This is my code, but it doesn’t work out.

	public GameObject boyGP2, dogGP2;
	

	void Update () {
	
	}
	
	void OnMouseDown(){
		if(gameObject.name=="boyGP2"){
			Destroy(dogGP2);
		}
		if(gameObject.name=="dogGP2"){
			Destroy(boyGP2);
		}
	}

Some things to check:

Is this script attached to both GameObjects in the editor? Otherwise the script isn’t running

In order for this to work you’d have to go to each GameObject’s inspector in the editor and fill the two variables with GameObjects. Otherwise the dogGP2 and boyGP2 variables will be blank.


Another way to solve the second tip is to define the variables at scene start like below:

// Attach this script to both objects

public GameObject boyGP2, dogGP2;

void Start() {
	boyGP2 = GameObject.Find("boyGP2");
	dogGP2 = GameObject.Find("dogGP2");
}

void OnMouseDown() {
       if(gameObject.name=="boyGP2"){
         Destroy(dogGP2);
       }
       if(gameObject.name=="dogGP2"){
         Destroy(boyGP2);
       }
}

I always code in UnityScript, but I’m pretty sure I got something as simple as this right.

First, make sure you have colliders on your game objects or else OnMouseDown() will not be called.

This script should be able to take care of it. Just attach it on both the game objects.

using UnityEngine;
using System.Collections;

public class DestroyOther : MonoBehaviour
{
	private GameObject other;
	
	void Start ()
	{
		switch (gameObject.name) {
		case "boyGP2":
			other = GameObject.Find ("dogGP2");
			break;
		
		case "dogGP2":
			other = GameObject.Find ("boyGP2");
			break;
			
		}
	
	}
	
	void OnMouseDown ()
	{
		Destroy (other);
	}

}