- Home /
The question is answered, right answer was accepted
Selecting and comparing two gameObj at runtime?
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);
}
}
}
Answer by Trlgger · Oct 03, 2015 at 09:26 PM
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.
I should add that you need to declare the List before adding elements of you will get a null reference exception
I can see what you did there and pretty nice idea. But I still don't understand how is that going to make me select the game Objects during the run time x.x
I'm not sure what you mean by select . Do you want to click on the game object and it becomes object1 and click on another that becomes object2?
Public boolean selected=false; Void On$$anonymous$$ouseDown(){selected=true;}
You need to assign a box collider to your object and set it to trigger. Then create a new script, put this function in the script and assign it to each object you wish to select.
private yourscriptname objectScript;
Add this to the top of your first script. Using the dynamic list option and adding all the objects to the list on start do the following in update(for now).
for(Int I =0;I < objectList.Count;I++){ objectScript = objectList[i].gameObj.GetComponent< yourscriptname > (); If(objectScript.selected){ selected1 = objectlist[I].gameObj; objectScript.selected = false; } }
Using this you can figure out how to select the way that best fits your needs.
Thanks a lot, I couldn't do exactly what I want. But I used the idea you gave me which is giving each object values and compare using the values ! Thank you!