Question by
Agard64 · Jul 26, 2017 at 02:08 PM ·
objectscomparison
How to check if any GameObject equals any other GameObject
So, I have a GoFish game that I am making in Unity and am trying to run a method that will check if any of the cards equal each other. I am competent in comparing just two GameObjects (I created a peek method to get the card's face value which was a component of the card object), but am unsure how I would compare all cards (in player1's deck) against each other at once in an easy and simplistic manner. For the record, I am comparing Substrings to do this and the GameObjects (cards) are stored in a separate script called GameManager.
Below is my code for both the peak method and the card comparison method (which is called makeBooks)
Peek
public static string peek(GameObject obj)
{
CardObject temp = obj.GetComponent<CardObject> ();
string tempString = temp.GetComponent<CardObject>().face.Substring(0,1);
return tempString;
}
makeBooks
public static void makeBooks()
{
if (CardMethod.peek (GameManager.pC0).Substring (0, 1) == CardMethod.peek(GameManager.pC1).Substring (0, 1)) {
Debug.Log (CardMethod.peek ("1-character card name is the same")); }
{
if (CardMethod.peek (GameManager.pC0).Substring (0, 2) == CardMethod.peek (GameManager.pC1).Substring (0, 2))
{
Debug.Log ("Two-character card name is the same");
}
}
}
Comment