Toggle Feature

Hello, my friend and myself have been working on this game but have encountered a small problem, a game breaking problem. WHat we want to do is use the Boolean in ToolSel to detect the state of the switch and when movement is false, the player shouldn’t be able to drag game objects around but should be able to preform another function, which hasn’t been created yet. We’ve tried creating an if statement on the OnMouseDown class to prevent the player from moving game objects but has resulted in said game objects disappearing when clicked on regardless of the state of the button. Thanks in advance.

MouseMovement Code

public class Move : MonoBehaviour {

    private Vector3 screenPoint;
    private Vector3 offset;   

    void OnMouseDown()
    {
        screenPoint = Camera.main.WorldToScreenPoint (gameObject.transform.position);
       
        offset = gameObject.transform.position - Camera.main.ScreenToWorldPoint (new Vector3 (Input.mousePosition.x, Input.mousePosition.y, screenPoint.z));
    }

    void OnMouseDrag()
    {
        Vector3 curScreenPoint = new Vector3(Input.mousePosition.x, Input.mousePosition.y, screenPoint.z);

        Vector3 curPosition = Camera.main.ScreenToWorldPoint(curScreenPoint) + offset;
        transform.position = curPosition;
    }   
}

ToolSel Code

public class ToolSel : MonoBehaviour {

    public Sprite s1;
    public Sprite s2;

    public static bool Movement;

    void OnMouseDown()
    {
        if (this.gameObject.GetComponent<SpriteRenderer>().sprite == s1) {
            gameObject.gameObject.GetComponent<SpriteRenderer> ().sprite = s2;
            Movement = false;
            return;
        }
        if (this.gameObject.GetComponent<SpriteRenderer>().sprite == s2) {
            gameObject.gameObject.GetComponent<SpriteRenderer> ().sprite = s1;
            Movement = true;
            return;
        }
    }
}

Check to make sure you don’t have two copies of ToolSel attached, with perhaps one of them having invalid (unassigned) sprites.

Also, you might want to refactor it so that the mouse going down is only checked in one place, and then that is passed around as an event that different things act upon, which can give you finer grain control of order of operation.