Jiji
1
Hey, So I am a beginner here. I am making a memory card game. I have a board with cards on it. I have wrote the script but I am kinda stuck, because I dont know how to select two GameObj during the runtime and compare them Comparing I think easy if I know how to select two GameObj Here is my code:
THIS SCRIPT WILL BE ATTACHED TO THE BOARD I THINK // I CAN EVEN ATTACH IT TO THE CARD RIGHT?
public class FlipAD : MonoBehaviour
{
public GameObject gameOb ; //SELECT DURING RUNTIME?
public GameObject gameOb1;//SAME?
void Start(){
//I think here where should I put my selecting two gameObjects function but I dont know how to write it?
}
void Update(){
}
void OnMouseDown(){
StartCoroutine (LoopRotation (180f)); //TO ROTATE GameOb 1
StartCoroutine (LoopRotation2 (180f)); //TO ROTATE GameOb 2 I know it's wrong (If someone can also give me a tip)
}
IEnumerator LoopRotation(float angle)
{
float rot = 0f;
float dir = 1f;
while(rot < angle)
{
gameOb.Rotate(new Vector3(0,90,0) * Time.deltaTime);
yield return null;
rot= rot+( 90f * Time.deltaTime);
CheckT ();
}
}
IEnumerator LoopRotation2(float angle)
{
float rot = 0f;
float dir = 1f;
while(rot < angle)
{
gameOb1.Rotate(new Vector3(0,90,0) * Time.deltaTime);
yield return null;
rot= rot+( 90f * Time.deltaTime);
CheckT ();
}
}
void CheckT(){ //TO COMPARE THE TWO OBJECTS
if (gameOb.tag == gameOb1.tag) {
Destroy(gameOb);
Destroy(gameOb1);
}
}
}
I wouldn’t compare the gameObjects, I would create an array of values and compare the elements (by index number) and create or destroy gameObjects based off the results.
Example1:
//set this at the top to allow lists
using System.Collections.Generic;
public class Checker : MonoBehaviour {
[System.Serializable]
public class ObjectData{
public GameObject gameObj;
public int gameObjValue = 0;
}
//Set these in the inspector
public List<ObjectData> objectList;
private bool checkOnce = false;
//Make sure not to compare an object with the same object
//Set these to the objects index you want to compare
public int compare1 = 0;
public int compare2 = 1;
void Start(){
//If you want to add objects dynamically
//objectDataList.Add(yourObject1);
//objectDataList.Add(yourObject2);
//Set the numbers you want to compare
//compare1 = 2;
//compare2 = 0;
}
void Update(){
//Check if rotation is less then specified angle
if(rot < angle){
//Turn toggle on
checkOnce = true;
}
if(checkOnce){
//Check Objects
Check();
//Turn the toggle off
checkOnce = false;
}
}
//Check rotation
void Check(){
if(objectList[compare1].gameObjValue == objectList[compare2].gameObjValue){
//Destroy objects
Destroy(objectList[compare1].gameObj);
Destroy(objectList[compare2].gameObj);
//Remove from list
objectList.RemoveAt(compare1);
objectList.RemoveAt(compare2);
}
}
}
You could create a class with a dynamic list like I did if you want to add and remove on the fly, or a simpler alternitive.
Example2:
public class Checker : MonoBehaviour {
public GameObject obj1;
public GameObject obj2;
public int objValue1 = 1;
public int objValue2 = 1;
void Start(){
}
void Update(){
//Check if rotation is less then specified angle
if(rot < angle){
//Turn toggle on
checkOnce = true;
}
if(checkOnce){
//Check Objects
Check();
//Turn the toggle off
checkOnce = false;
}
}
//Check rotation
void Check(){
if(objValue1 == objValue2){
Destroy(obj1);
Destroy(obj2);
}
}
}
Never tested the scripts but you get the point.
I would also look into object pooling instead of destroying objects if you plan on doing it all the time.