3
\$\begingroup\$

I created a simple animation.

I have a "Gamer" object, which I move and hit squares to destroy them.

Squares I have to destroy in the right order (1-2-3-4-5 etc.)

If I make a mistake, the animation comes to an end.

However, I would like to change this code now. I would like to change OnCollisionEnter2D() on OnMouseDown().

I would like to continue to delete the squares one by one, but instead of using a collision - simply click on the squares.

Could anyone help you convert this code?

This is my code posted to the "Gamer" object:

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

    public class Gamer : MonoBehaviour {

    int index =1;
    public GameObject gameOverText, restartButton ;

    public GameObject WinningText, ExitButton, NextLevelButton ;

    public GameObject Manager;


    void Start () {
        gameOverText.SetActive (false);
        restartButton.SetActive (false);
        Manager.SetActive (true);
        WinningText.SetActive (false);
        ExitButton.SetActive (false);
        NextLevelButton.SetActive (false);
    }
    public void OnCollisionEnter2D (Collision2D col)
    {
        Debug.Log("Player hit object: " + col.transform.name);
        //check if the object you hit is one of the "collisionObjects":
        if(col.transform.GetComponent<collisionObject>())
            GetComponent<AudioSource>().Play();
        {
            collisionObject colObj = col.transform.GetComponent<collisionObject>();
            if(!colObj) return;
            if(index == colObj.myIndexNumber) {
                if (colObj.isHighestNumber) {
                    Debug.Log ("Yay you correctly hit the last Square, You WIN!");
                    WinningText.SetActive (true);
                    NextLevelButton.SetActive (true);
                    ExitButton.SetActive (true);
                    gameObject.SetActive (false);
                    Manager.SetActive (false);
                }
                Destroy(col.gameObject);
                index++;

            }
            // here, we know it didn't match the number. :)
            else  {
                gameOverText.SetActive (true);
                restartButton.SetActive (true);
                gameObject.SetActive (false);
                Manager.SetActive (false);

            }
        }    
    }
}

And this is my code placed in the "square" object:

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

        public class collisionObject : MonoBehaviour {
           public int myIndexNumber;
           public bool isHighestNumber;
           public void Start()
           {

        isHighestNumber = true;
        //find all other objects:
        foreach (collisionObject cObj in GameObject.FindObjectsOfType<collisionObject>()) {

            //check if any of the other objects has a larger number than mine, if yes, note to self that i am not the highest number.
            if (cObj.myIndexNumber > myIndexNumber) {
                isHighestNumber = false;
                break;
            }
        }
        Debug.Log("Object number " + myIndexNumber + " is here // is highest Number: " + isHighestNumber);
    }
}
\$\endgroup\$
0
\$\begingroup\$

Move the OnMouseDown function to the square objects. Inside of it, call something like gamer.ClickedSquare(myIndexNumber); where gamer is your hero (I assume). Note that you should give a reference of the gamer to each square for this to work.

Then on the Gamer class, convert your OnCollisionEnter2D(...) with ClickedSquare(int index) (or any other name you want to give it). This function will be called every time a square is clicked, and give it's index number, then it's up to the gamer object to decide if the index is the expected one, or to ignore it if it's the wrong one.

\$\endgroup\$
0
\$\begingroup\$

Inherit the object on which you want to detect mouse click with EventSystems.IPointerClickHandler and move your code from OnCollisionEnter2D to void OnPointerClick()

You'll need to add PhysicsRaycaster component on the camera to detect mouseovers/clicks

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.