- Home /
Checking Tiles For Match Problem Using If Statements
Howdy all. Got a quick one as i am stuck. I have this code for a tile match game. Now the code works for the following matches only. The Numbers Signify the tile in Squence
1,2,3,4,5
2,3,4,5
3,4,5
it does not work for the following matches
1,2,3,4
1,2,3
2,3,4
here is the code. Any help would be appreciated.
void CheckForTileMatch () {
if (OneOne.renderer.material.name == OneTwo.renderer.material.name) {
if (OneTwo.renderer.material.name == OneThree.renderer.material.name) {
if (OneThree.renderer.material.name == OneFour.renderer.material.name) {
if (OneFour.renderer.material.name == OneFive.renderer.material.name) {
Debug.Log ("You have a 5 x Match!!!");
}
}
}
} else if (OneOne.renderer.material.name == OneTwo.renderer.material.name) {
if(OneTwo.renderer.material.name == OneThree.renderer.material.name) {
if (OneThree.renderer.material.name == OneFour.renderer.material.name){
Debug.Log ("You have a 4 x Match!!!");
}
}
} else if (OneTwo.renderer.material.name == OneThree.renderer.material.name) {
if(OneThree.renderer.material.name == OneFour.renderer.material.name) {
if(OneFour.renderer.material.name == OneFive.renderer.material.name) {
Debug.Log ("You have a 4 x Match!!!");
}
}
} else if (OneOne.renderer.material.name == OneTwo.renderer.material.name) {
if(OneTwo.renderer.material.name == OneThree.renderer.material.name) {
Debug.Log ("You have a 3 x Match!!!");
}
} else if (OneTwo.renderer.material.name == OneThree.renderer.material.name) {
if(OneThree.renderer.material.name == OneFour.renderer.material.name) {
Debug.Log ("You have a 3 x Match!!!");
}
} else if (OneThree.renderer.material.name == OneFour.renderer.material.name) {
if(OneFour.renderer.material.name == OneFive.renderer.material.name) {
Debug.Log ("You have a 3 x Match!!!");
}
}
}
also if anyone can suggest a more efficient way of doing this i am all for it :).
Answer by HarshadK · Jan 06, 2015 at 01:06 PM
Are you looking for this?
void CheckForTileMatch () {
int matches = 1;
if (OneOne.renderer.material.name == OneTwo.renderer.material.name) {
matches++;
}
if (OneTwo.renderer.material.name == OneThree.renderer.material.name) {
matches++;
}
if (OneThree.renderer.material.name == OneFour.renderer.material.name) {
matches++;
}
if (OneFour.renderer.material.name == OneFive.renderer.material.name) {
matches++;
}
Debug.Log("You have a "+matches+" x Match!!!");
}
Close but no exactly what i was looking for. I need to know exactly what gameObjects were matched so i can destroy and instantiate new ones. kinda like so.
if (OneOne.renderer.material.name == OneTwo.renderer.material.name) {
if (OneTwo.renderer.material.name == OneThree.renderer.material.name) {
if (OneThree.renderer.material.name == OneFour.renderer.material.name) {
if (OneFour.renderer.material.name == OneFive.renderer.material.name) {
Debug.Log ("You have a 5 x $$anonymous$$atch!!!");
DestroyObject (OneOne);
DestroyObject (OneTwo);
DestroyObject (OneThree);
DestroyObject (OneFour);
DestroyObject (OneFive);
Playerscore =+ 1000;
//Instantiate new objects...
}
}
}
}
Something like this:
void CheckForTile$$anonymous$$atch () {
bool oneOne$$anonymous$$atch = false;
bool oneTwo$$anonymous$$atch = false;
bool oneThree$$anonymous$$atch = false;
bool oneFour$$anonymous$$atch = false;
int matches = 1;
if (OneOne.renderer.material.name == OneTwo.renderer.material.name) {
matches++;
oneOne$$anonymous$$atch = true;
}
if (OneTwo.renderer.material.name == OneThree.renderer.material.name) {
matches++;
oneTwo$$anonymous$$atch = true;
}
if (OneThree.renderer.material.name == OneFour.renderer.material.name) {
matches++;
oneThree$$anonymous$$atch = true;
}
if (OneFour.renderer.material.name == OneFive.renderer.material.name) {
matches++;
oneFour$$anonymous$$atch = true;
}
Debug.Log("You have a "+matches+" x $$anonymous$$atch!!!");
if(oneOne$$anonymous$$atch) {
DestroyObject (OneOne);
DestroyObject (OneTwo);
}
if(oneTwo$$anonymous$$atch)
{ DestroyObject (OneThree); }
if(oneThree$$anonymous$$atch)
{ DestroyObject (OneFour); }
if(oneFour$$anonymous$$atch)
{ DestroyObject (OneFive); }
}
Your answer
Follow this Question
Related Questions
How to set different score values to different prefabs of an array? 0 Answers
Distance sensor 1 Answer
why != is different from var1 = !var2? 2 Answers
Check Material C# 1 Answer