2

I want to make a Simple Memory Game using C# on Unity. It is a 2D game. I am trying to make a game in which the PC player clicks in a card, and then the game needs to call the function memory(GameObject card), but I don't know how to do and I can't seen to find it in the internet. How do I do that? My code is:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MemoryGame : MonoBehaviour
{

    private int counterOverturnedCards;
    private GameObject[] overturnedCards = new GameObject[2];
    public Sprite back, krunk, nash, smallNorm, bigNorm, geary, emperorVeloXXVII, realVelo, nTrance, nitrosOxide, zam, zem, mixvelo;

    // Start is called before the first frame update
    void Start()
    {
        gameObject.GetComponent<SpriteRenderer>().sprite = back;
        counterOverturnedCards = 0;
    }

    // Update is called once per frame
    void Update()
    {

    }

    void OnMouseDown()
    {
        if (gameObject.GetComponent<SpriteRenderer>().sprite == back && (counterOverturnedCards == 0 || counterOverturnedCards == 1))
        {
            if (gameObject == GameObject.Find("Cards/card10") || gameObject == GameObject.Find("Cards/card20"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = krunk;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card03") || gameObject == GameObject.Find("Cards/card21"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = nash;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card05") || gameObject == GameObject.Find("Cards/card13"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = geary;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card04") || gameObject == GameObject.Find("Cards/card24"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = mixvelo;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card06") || gameObject == GameObject.Find("Cards/card18"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = smallNorm;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card12") || gameObject == GameObject.Find("Cards/card22"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = bigNorm;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card01") || gameObject == GameObject.Find("Cards/card14"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = emperorVeloXXVII;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card08") || gameObject == GameObject.Find("Cards/card17"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = realVelo;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card07") || gameObject == GameObject.Find("Cards/card19"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = nTrance;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card09") || gameObject == GameObject.Find("Cards/card15"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = zam;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card11") || gameObject == GameObject.Find("Cards/card16"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = zem;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
            else if (gameObject == GameObject.Find("Cards/card02") || gameObject == GameObject.Find("Cards/card23"))
            {
                gameObject.GetComponent<SpriteRenderer>().sprite = nitrosOxide;
                overturnedCards[counterOverturnedCards] = gameObject;
                counterOverturnedCards++;
            }
        }
        if (counterOverturnedCards == 2)
        {
            if (overturnedCards[0].GetComponent<SpriteRenderer>().sprite != overturnedCards[1].GetComponent<SpriteRenderer>().sprite)
            {
                StartCoroutine(waitTwoSeconds());
                overturnedCards[0].GetComponent<SpriteRenderer>().sprite = back;
                overturnedCards[1].GetComponent<SpriteRenderer>().sprite = back;
            }
            counterOverturnedCards = 0;
        }
    }

    IEnumerator waitTwoSeconds()
    {
        yield return new WaitForSeconds(2);
    }
}

4 Answers4

2

You have two options:

1) Physics2D.Raycast, this may work perfectly for your game since you have a single script that should detect all the cards.

void Update () {
         if (Input.GetMouseButtonDown(0)) {
             CastRay();
         }       
     }

     void CastRay() {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit2D hit = Physics2D.Raycast (ray.origin, ray.direction, Mathf.Infinity);
         if (hit.collider !=null) {
             Debug.Log (hit.collider.gameObject.name);
     }

2) OnMouseDown, you can attach a script to all of your cards with OnMouseDown method in them.

  void OnMouseDown(){
     Debug.Log (this.gameObject.name);
  } 

Both options will give you the name of the object, not much of a difference especially for a small game. The main difference is that Physics2D.Raycast works only for 2D colliders. On the other hand, OnMouseDown is generic.

Please note that you have to have 2D colliders on your game objects for both of the solutions.

Onurcan Onder
  • 496
  • 2
  • 6
  • @EduardodeQuadros, as I stated in my answer, if your code is attach to every single card object, then ‘OnMouseDown ‘ should work. However, if it’s just attached to the main camera, then use Raycast. In any case, just make sure tou have 2D colliders on every single card sprite. – Onurcan Onder May 08 '20 at 13:52
1

A possible solution would be assigning a 2D collider to your card and attach a script to it, with an OnMouseDown() function.

For example (still haven't tested it, though):

public class Card : MonoBehaviour {

public Card(){
        // Constructor, modify it depending on what you need
    }

void OnMouseDown()
    {
        // Find the game object with the "memoria" script as you please. In this example 
        // you find it by name but there are many ways to do this
        GameObject myMemory =  GameObject.Find("Name of the gameobject with the memoria script");
        myMemory.GetComponent<memoria>().memory(gameObject); //Call the memory method and pass the clicked gameObject
    }
}
Pipimi
  • 133
  • 8
0

You could use RaycastHit to trap the GameObject for example:(So you have to put a collider on GameObject)

In Game 3D:

     if (Input.GetMouseButtonDown(0))
     {
         Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
         RaycastHit rayHit;
         if (Physics.Raycast(ray, out rayHit, 100.0f)){
             if(rayHit.collider.tag == "Something") //I test tag, but you could what you want..
             {
                 var GameObjClicked  = rayHit.collider.gameObject;
             }
         }
     }

rayHit.collider.gameObject gives you the gameobject Clicked

To adapt the script with 2D:

    if (Input.GetMouseButtonDown(0)) {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
        Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

        RaycastHit2D rayHit = Physics2D.Raycast(mousePos2D, Vector2.zero);
        if (rayHit.collider != null) {
            var gameobjClicked = rayHit.collider.gameObject
        }
    }
Frenchy
  • 16,386
  • 3
  • 16
  • 39
0

i would use a raycast to get what object you hit. you can store it in the cast Ray ray = new Ray(transform.position + offset, dir);

    Debug.DrawRay(transform.position + offset, dir * sightDis, Color.green);
            if (Physics.Raycast(ray, out RaycastHit hit,sightDis * sightDis))
            {
              //  print(hit);

                if (hit.transform == attackTarget) return true;
                //clear line of vision


                // if distances < thershold && raycast from player to boss hits..
                // transtion to prsue

            }
Jesse Webb
  • 11
  • 1