What is wrong with my Code? (MouseDown event doesn't work)

First of all the code:

void OnMouseDown()
    {
        if (clicked.Count < 2)
        {
            gameObject.renderer.enabled = false;
            clicked.Add(gameObject);
            if (clicked.Count == 2)
            {
                if (clicked[0].name == clicked[1].name)
                {
                    Destroy(clicked[0]);
                    Destroy(clicked[1]);
                    clicked.Clear();
                }
            }
        }
    }

the point is i have a sphere with extern objects (pokemon figures) as childrin in the sphere
and if you click on the sphere it should disappear.
if you have clicked on two spheres the two clicked spheres should be compared and if they have the same figure in it both should be deleted

for better understanding it’s a memory game^^

but the problem is the mousedown event don’t work
if i set a text in the first line everything else doesn’t work i don’t know why…

“okay this is a little bit much for recognizing a mousedown event”

Yes, it really is this difficult. Imagine you are standing at a window. The mountains in the distance span miles, but you might only see 30’ close to the window. With Unity you are looking into a 3D environment, so the way to find things is to cast a ray. Here is an example script you can attach to an empty game object. Put in a scene with some other game objects and click on things:

using UnityEngine;
using System.Collections;

public class RaycastExample : MonoBehaviour {

	void Update () {
		if (Input.GetMouseButtonDown (0))
		{
			Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
			RaycastHit hit;
			if (Physics.Raycast(ray, out hit))
			{
				Debug.Log ("Name = " + hit.collider.name);
				Debug.Log ("Tag = " + hit.collider.tag);
				Debug.Log ("Hit Point = " + hit.point);
				Debug.Log ("Normal = " + hit.normal);
				Debug.Log ("Object position = " + hit.collider.gameObject.transform.position);
				Rigidbody rb = hit.collider.rigidbody;
				
				if (rb != null)
					Debug.Log ("Velocity = " + rb.velocity);
				Debug.Log ("--------------");
			}
		}
	}
}

Now here is a bit of rewriting of your code. Since I don’t have your environment to test it on, there are likely issues. Since you seem to understand programming I made some changes that were not related to the immediate problem but might give you some ideas about C# and unity. Comments:

  • You can add a “using System.Collections.Generic;” at the top of your file and then not have to specify the full class hierarchy for the generic lists.

  • Unity as a build-in Random class that is seeded by Unity. No need to create an instance of System.Random.

  • Typically I only use the List class to store information if I don’t know how many items I need to store. I would have used the built-in array class for all this code. I just converted the PokeNames array to a build-in array.

  • You will need to tag your spheres with “Sphere” for this code to work. You can read more about tags [here][1].

    using UnityEngine;
    using System.Collections;
    using System.Collections.Generic;

     public class NewBehaviourScript1 : MonoBehaviour
     {
         List<GameObject> SphereList;
         List<GameObject> Pokemon;
         List<GameObject> clicked = new System.Collections.Generic.List<GameObject>();
         List<int> index;
     
         string[] PokeNames = new string[] {"Bisaflor","Bisasam","Celebi", "Chelast", "Chelterra", "Darkrai", "Flemmli", "Floink", 
     	    "Glumanda", "Glurak", "Groudon", "Hydropi", "Impergator", "Impoleon", "Jirachi", "Karnimani", "Lohgock",
     		"Mew", "Panferno", "Panflam", "Pikachu", "Plinfa", "Pliprin", "Schiggy", "Suicune", "Turtok", "Zekrom" }; 
     
         TextMesh t;
    
         void Start()
         {
             SphereList = new System.Collections.Generic.List<GameObject>();
             Pokemon = new System.Collections.Generic.List<GameObject>();
             index = new System.Collections.Generic.List<int>();
     	
             GameObject[] sphere = FindObjectsOfType(typeof(GameObject)) as GameObject[];
             
             for (int i = 0; i < sphere.Length; i++)
             {
                 if (sphere*.name == "Sphere")*
    

{
SphereList.Add(sphere*);*
}
}
for (int i = 0; i < 27; i++)
{
Pokemon.Add(GameObject.Find(“/”+PokeNames*));*
}
for (int i = 0; i < 27; i++)
{
index.Add(i);
index.Add(i);
}

for (int i = SphereList.Count - 1; i >= 0; i–)
{
int value = Random.Range(0, i + 1);
SphereList*.name = index[value].ToString();*
index.RemoveAt(value);
}
for (int i = 0; i < 27; i++)
{
for (int j = 0; j < 54; j++)
{
if(SphereList[j].name==i.ToString())
Pokemon*.transform.parent = SphereList[j].transform;*
}
if(Pokemon*.name==“Darkrai”)*
Pokemon.transform.position = new Vector3(Pokemon_.transform.parent.transform.position.x, Pokemon*.transform.parent.transform.position.y - 14.0f, 0.0f);_
_else if (Pokemon.name == “Flemmli”)
Pokemon.transform.position = new Vector3(Pokemon.transform.parent.transform.position.x, Pokemon.transform.parent.transform.position.y - 8.0f, 0.0f);_

_else if (Pokemon.name == “Mew”)
Pokemon.transform.position = new Vector3(Pokemon.transform.parent.transform.position.x, Pokemon.transform.parent.transform.position.y - 26.0f, 0.0f);_

_else if (Pokemon.name == “Chelast”)
Pokemon.transform.position = new Vector3(Pokemon.transform.parent.transform.position.x, Pokemon.transform.parent.transform.position.y - 8.0f, 0.0f);
else
Pokemon.transform.position = new Vector3(Pokemon.transform.parent.transform.position.x, Pokemon.transform.parent.transform.position.y - 7.0f, 0.0f);
}
}*_

void Update()
{
* if (Input.GetMouseButtonDown (0))*
* {*
* RaycastHit hit;*
* Ray ray = Camera.main.ScreenPointToRay (Input.mousePosition);*
* if (Physics.Raycast (ray, out hit))*
* {*
* if (hit.collider.tag == “Sphere”)*
* {*
* if (clicked.Count < 2)*
* {*
* clicked.Add(hit.collider.gameObject);*
* hit.collider.gameObject.renderer.enabled = false;*
* hit.collider.enabled = false; *
* if (clicked.Count == 2)*
* {*
* if (clicked[0].name == clicked[1].name)*
* {*
* Destroy(clicked[0]);*
* Destroy(clicked[1]);*
* }*
* else*
* {*
* clicked[0].renderer.enabled = true;*
* clicked[0].collider.enabled = true;*
* clicked[1].renderer.enabled = true;*
* clicked[1].collider.enabled = true;*
* }*
* clicked.Clear ();*
* }*
* }*
* }*
* }*
* }*
}
}

_*[1]: http://docs.unity3d.com/Documentation/Components/Tags.html*_