Help with OnMouseDown()!

I wanna do a logic game called number maze 6x6 (you can play it if you search it in Google). I create the class Casilla and in this, in OnMouseDown() but it doesn’t work :face_with_spiral_eyes:. Can somebody help me please? :frowning:

#pragma strict
#pragma downcast

public class Casilla{

var elemento:Transform;

var indice: int;//11-16;21-26;31-36;...;61-66;
var posibilidad1:Transform;
var posibilidad2:Transform;
var posibilidad3:Transform;
var posibilidad4:Transform;
var estaSeleccionada:boolean=false;
var leTocaThis:boolean=false;
var elegida:boolean=false;
var estaAzul:boolean=false;



function Awake(){
}
function Casilla(){
}
function OnMouseDown(){

if(leTocaThis  estaAzul){
elemento.renderer.material.color=Color.red;
estaSeleccionada=true;
elegida=true;
}
if(!leTocaThis){
//ver mensaje error
print("¡Aquí no puedes mover!¡Solo puedes ir a las casillas iluminadas en azul!");
}
}
function setEstaAzul(res){
estaAzul=res;
}
function setLeTocaThis(res){
leTocaThis=res;
}
function getLeTocaThis(){
return leTocaThis;
}
function getElegida(){
return elegida;
}
function ReseteaElegida(){//Se pone a true solo al cumplir las condiciones en OnMouseDown().
elegida=false;
}
function getElemento(){
return elemento;
}
function getPosibilidad1(){
return posibilidad1;
}
function getPosibilidad2(){
return posibilidad2;
}
function getPosibilidad3(){
return posibilidad3;
}
function getPosibilidad4(){
return posibilidad4;
}
function getEstaSeleccionada(){
return estaSeleccionada;
}
function setEstaSeleccionada(res){
estaSeleccionada=res;
}
function getIndice(){
return indice;
}
}

var casillaActual:int=0;
var puzzle:Casilla[];
var usuarioHaElegido:boolean=false;
var posibilidad1:Casilla;
var posibilidad2:Casilla;
var posibilidad3:Casilla;
var posibilidad4:Casilla;
var numeroPosibilidades:int=1;
var elegida:Casilla;


//Funciones de Unity modificadas para el Juego

function Awake () {

puzzle[casillaActual].getElemento().renderer.material.color=Color.red;
IluminaCasillasAceptadas();
puzzle[casillaActual].setLeTocaThis(true);
puzzle[casillaActual].setEstaSeleccionada(true);
AsociaPosibilidadesYCasillas();
}


function Start () {


}

function Update () {

if(puzzle[35].getEstaSeleccionada()){
print("¡Enhorabuena!¡Has ganado!");
}
for(var e in puzzle){//Si no ha llegado a la casilla final, se busca la siguiente elegida.

usuarioHaElegido= e.getElegida();
if(usuarioHaElegido){
elegida=e;
puzzle[casillaActual].setLeTocaThis(false);
puzzle[casillaActual].setEstaSeleccionada(false);
e.ReseteaElegida();

	
//Ahora apagaremos todas las que eran posibilidad
	if(elegida.Equals(posibilidad1)){
	puzzle[posibilidad1.getIndice()].setLeTocaThis(true);
	puzzle[posibilidad1.getIndice()].setEstaSeleccionada(true);
	puzzle[posibilidad2.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad2.getIndice()].setEstaAzul(false);
	puzzle[posibilidad2.getIndice()].setLeTocaThis(false);
	puzzle[posibilidad3.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad3.getIndice()].setEstaAzul(false);
	puzzle[posibilidad3.getIndice()].setLeTocaThis(false);
	puzzle[posibilidad4.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad4.getIndice()].setEstaAzul(false);
	puzzle[posibilidad4.getIndice()].setLeTocaThis(false);
	}
	if(elegida.Equals(posibilidad2)){
	puzzle[posibilidad2.getIndice()].setLeTocaThis(true);
	puzzle[posibilidad2.getIndice()].setEstaSeleccionada(true);
	puzzle[posibilidad1.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad1.getIndice()].setEstaAzul(false);
	puzzle[posibilidad1.getIndice()].setLeTocaThis(false);
	puzzle[posibilidad3.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad3.getIndice()].setEstaAzul(false);
	puzzle[posibilidad3.getIndice()].setLeTocaThis(false);
	puzzle[posibilidad4.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad4.getIndice()].setEstaAzul(false);
	puzzle[posibilidad4.getIndice()].setLeTocaThis(false);
	}
	if(elegida.Equals(posibilidad3)){
	puzzle[posibilidad3.getIndice()].setLeTocaThis(true);
	puzzle[posibilidad3.getIndice()].setEstaSeleccionada(true);
	puzzle[posibilidad1.getIndice()].getElemento().renderer.material.color =Color.white;
	puzzle[posibilidad1.getIndice()].setEstaAzul(false);
	puzzle[posibilidad1.getIndice()].setLeTocaThis(false);
	puzzle[posibilidad2.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad2.getIndice()].setEstaAzul(false);
	puzzle[posibilidad2.getIndice()].setLeTocaThis(false);
	puzzle[posibilidad4.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad4.getIndice()].setEstaAzul(false);
	puzzle[posibilidad4.getIndice()].setLeTocaThis(false);
	}
	if(elegida.Equals(posibilidad4)){
	puzzle[posibilidad4.getIndice()].setLeTocaThis(true);
	puzzle[posibilidad4.getIndice()].setEstaSeleccionada(true);
	puzzle[posibilidad1.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad1.getIndice()].setEstaAzul(false);
	puzzle[posibilidad1.getIndice()].setLeTocaThis(false);
	puzzle[posibilidad3.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad3.getIndice()].setEstaAzul(false);
	puzzle[posibilidad3.getIndice()].setLeTocaThis(false);
	puzzle[posibilidad2.getIndice()].getElemento().renderer.material.color=Color.white;
	puzzle[posibilidad2.getIndice()].setEstaAzul(false);
	puzzle[posibilidad2.getIndice()].setLeTocaThis(false);
	}
	casillaActual=e.getIndice();
	IluminaCasillasAceptadas();
	AsociaPosibilidadesYCasillas();
	e.ReseteaElegida();
	break;	
}
}
}

//Métodos creados para el algoritmo



function IluminaCasillasAceptadas(){//sólo toca los transform

//Como mínimo, cada pelota tiene 1 casilla a la que poder ir
puzzle[casillaActual].getPosibilidad1().renderer.material.color=Color.cyan;

if(puzzle[casillaActual].getPosibilidad2()!=null){
puzzle[casillaActual].getPosibilidad2().renderer.material.color=Color.cyan;
numeroPosibilidades++;
}
if(puzzle[casillaActual].getPosibilidad3()!=null){
puzzle[casillaActual].getPosibilidad3().renderer.material.color=Color.cyan;
numeroPosibilidades++;
}
if(puzzle[casillaActual].getPosibilidad4()!=null){
puzzle[casillaActual].getPosibilidad4().renderer.material.color=Color.cyan;
numeroPosibilidades++;
}
}
function AsociaPosibilidadesYCasillas(){
var contador:int=0;
for(var e in puzzle){
if(e.getElemento().renderer.material.color==Color.cyan){
	if(e.getElemento().Equals(puzzle[casillaActual].getPosibilidad1())){
	e.setEstaAzul(true);
	e.setLeTocaThis(true);
	posibilidad1=e;
	contador++;
	}
	if(e.getElemento().Equals(puzzle[casillaActual].getPosibilidad2())){
	e.setEstaAzul(true);
	e.setLeTocaThis(true);
	posibilidad2=e;
	contador++;
	}
	if(e.getElemento().Equals(puzzle[casillaActual].getPosibilidad3())){
	e.setEstaAzul(true);
	e.setLeTocaThis(true);
	posibilidad3=e;
	contador++;
	}
	if(e.getElemento().Equals(puzzle[casillaActual].getPosibilidad4())){
	e.setEstaAzul(true);
	e.setLeTocaThis(true);
	posibilidad4=e;
	contador++;
	}
	if(contador==numeroPosibilidades){
	break;
	}
}
}
}

Thanks for your attention :slight_smile:

English variables would be much easier to read :slight_smile:

OnMouseDown works only on the gameobject that the script is added to.
Some more clarification on what its supposed to do and what kind of error it produces would be very helpful.

you use onmousedown inside if statements. Also I think you have to send it a number for which mouse button.

OnMouseDown doesn’t take a button parameter, Input.GetMouseButtonDown does though…

I didn’t put any error because it hasn’t got! But when I played the game, it doesn’t work…
It’s not a button, there is a puzzle with Text3D and colliders (it has got inside numbers).

1194942–47319–$algoritmoJuego.js (6.76 KB)
1194942–47320–$Prueba.7z (7.62 KB)

I send U my scene and my script, the variables are in Spanish because I’m a Spaniard but the most words are similars! posibilidad1=possibility1(squares that u can go),indice=index, EstaAzul()=isBlueNow() [The squares will light up when they can be visited(possibility) and red when u are in one],
casillaActual=currentSquare, getIndice()=getIndex(), IluminaCasillasAceptadas()=LightUpPossibilities()[Squares that we can go will light up blue], AsociaPosibilidadesYCasillas()=AssociatePossibilities&Squares()[possibilities are Transforms and I needed Squares to use their functions, then I created this method to associate Transforms and Squares(Casillas in Spanish)], userHaElegido=userChose,…

The print when u’ll arrive at Square [35], is the end of the game and I put in the print= Congratulations!! U win!

Well, how can I do the type Square with one function OnMouseDown() and then I put it in the array without I have to do for 36 elements one Script with the method OnMouseDown() inside?

I put the link of the right working of the game xD:
http://www.logicmazes.com/n1mz.html

And thx again! :slight_smile: