-1

So I have been making this 2D game in Unity and it has buttons on the left all the time. The buttons are actually objects with no text and a sprite, which are supposed to stay on one place and activate a function on click (the function works).

I have tried using this simple script, but it appears to not be working, even after adding hitboxes/rigibodies to the object: { public int TerraformClick;

void Update()
{
    if(Input.GetMouseButtonDown(1) == true)
        {
            float TerraformClick = 1;
            print("Changed TerraformClick");
        }
}

So I just want to ask what the errors are, if there are some (none popped out)

Waterpig
  • 3
  • 1
  • 1
    Button 1 is 'secondary button' (typically right click). Button 0 is the normal left click. Does this fix it? – ryeMoss Feb 20 '19 at 17:28
  • First of all: what ryeMoss said, also: do you want to press the objects substituting buttons or do you want it to happen everytime you hit the left mouse button? – bastingup Feb 20 '19 at 17:39
  • so the other buttons you are talking about are `UI.Button` or `UI.Image` components? Maybe you can checkout [OnMouseDown](https://docs.unity3d.com/ScriptReference/MonoBehaviour.OnMouseDown.html) – derHugo Feb 20 '19 at 18:11
  • This code detects when the mouse button is pressed with no regards to *where* the mouse is. – Draco18s no longer trusts SE Feb 20 '19 at 19:32

1 Answers1

0

This code would detect if you clicked a specific object.

void OnMouseDown()
{
     Destroy (this.gameObject);
} 

The object would need a rigidbody and a collider of some sort.

AriBaa Games
  • 518
  • 2
  • 7