OnMouseButtonDown / Raycast

Hello,
I am trying to apply a script to an object for a mobile game. The object will start in the initial state where the meshrenderer is false. (invisible) When clicked, the object will go to the visible state. When clicked again, the object will rotate with an animation. When clicked for the third time third time, the object will go back to the initial state (invisible). The cycle is to repeat after this. My problem is that this works on a single object. However, when I apply the script to more than one object, they all interact at the same time. I only want the object clicked to go through the state cycles. I will have about 100 of these objects in the game at once.

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

public class reflectorclickaction : MonoBehaviour {

    private enum ReflectorState {Initial, Visible, Rotate};
    private ReflectorState myState;
    protected Animation Animation;
    RaycastHit hit;
    protected void Awake()
     {
         Animation = GetComponent<Animation>();
     }

    void Start() {
        myState = ReflectorState.Initial;
    }

    void Update() {
        if (myState == ReflectorState.Initial) {
            state_Initial();
        }
        else if (myState == ReflectorState.Visible) {
            state_Visible();
        }
        else if (myState == ReflectorState.Rotate) {
            state_Rotate();
        }
    }
    void state_Initial() {
        myState = ReflectorState.Initial;
        gameObject.GetComponentInChildren<Renderer>().enabled = false;
        gameObject.GetComponentInChildren<MeshCollider>().enabled = false;
        if (Input.GetMouseButtonDown(0)) {
            Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit rhInfo;
            bool didHit = Physics.Raycast(toMouse, out rhInfo, 500.0f);
            if (didHit) {
                Debug.Log(rhInfo.collider.name + " -- " + rhInfo.point);
                myState = ReflectorState.Visible;
                Debug.Log("State is set to Visible.");
                }
                else {
                    Debug.Log("No Object Hit");
                }
            }
        }
    void state_Visible() {
        gameObject.GetComponentInChildren<Renderer>().enabled = true;
        gameObject.GetComponentInChildren<MeshCollider>().enabled = true;
        if (Input.GetMouseButtonDown(0)) {
            Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit rhInfo;
            bool didHit = Physics.Raycast(toMouse, out rhInfo, 500.0f);
            if (didHit) {
                Debug.Log(rhInfo.collider.name + " -- " + rhInfo.point);
                myState = ReflectorState.Rotate;
                Debug.Log("State is set to Rotate.");
                Animation.Play("ReflectorRotate", PlayMode.StopAll);
            }
                else {
                    Debug.Log("No Object Hit");
                }
            }
        }
    void state_Rotate() {
        if (Input.GetMouseButtonDown(0)) {
            Ray toMouse = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit rhInfo;
            bool didHit = Physics.Raycast(toMouse, out rhInfo, 500.0f);
            if (didHit) {
                Debug.Log(rhInfo.collider.name + " -- " + rhInfo.point);
                myState = ReflectorState.Initial;
                Animation.Play("ReflectorRotateBack", PlayMode.StopAll);
                Debug.Log("State is set to Initial");
            }
                else {
                    Debug.Log("No Object Hit");
                }
            }
        }
    }

Hi matthewcastiglia,

You’re not checking if the mouse is being clicked on the current object when you’re checking if the raycast hit something. Try making the following change to your if-statements checking to see if “didHit” was true.

if (didHit.gameObject == gameObject)

Hope this helps!

I added that if-statement and it still does not work. It is doing the same as before:face_with_spiral_eyes: