0

Here's the code I'm having problems with:

public class PlaneBehaviour : MonoBehaviour {
    public GameObject ClickSymbol;

    public void Update() {
        if (Input.GetMouseButtonDown(0)) {
            Instantiate(ClickSymbol, Input.mousePosition, Quaternion.identity);
        }
    }
}

When user clicks the left mouse button, then multiple ClickSymbol's will be instantiated. How to prevent that behavior (create the object only once) ?

Marek M.
  • 3,799
  • 9
  • 43
  • 93
  • Holding down the mouse button is likely a stream of clicks rather than just 1. Would using a singleton work? Then you can dispose of it when you let go of the mouse button – Joe Phillips Jul 20 '17 at 21:50
  • But I'm not holding down the mouse button, I even tried with other mouse - the same effect. – Marek M. Jul 20 '17 at 21:54
  • Put `Debug.Log` inside the if statement and see how many times the log comes out each time the Button is pressed – Programmer Jul 20 '17 at 21:56
  • I've already put a breakpoint in there and I see it being hit like 50 times (I didn't count it exactly, but that would be somewhere around that number). – Marek M. Jul 20 '17 at 22:02
  • Is ClickSymbol a prefab? If yes, is the PlaneBehaviour script attached to this ClickSymbol prefab or any of its child? – Programmer Jul 20 '17 at 22:03
  • ClickSymbol is a prefab, yes. As for the PlaneBehaviour script, it's attached to multiple (not intersecting, not overlapping) objects on a plane. – Marek M. Jul 20 '17 at 22:10

1 Answers1

3

As for the PlaneBehaviour script, it's attached to multiple (not intersecting, not overlapping) objects on a plane

That's what I thought you are doing and wanted to verify this before adding an answer. Joe's answer should work but this is a bad design.

You get the multiple Objects instead of one because the script is attached to multiple Objects.

The proper solution here is to have one script that reads from the Input then call a function or trigger an event in another script if required. Create a new script called InputReader or with similar name then move your if (Input.GetMouseButtonDown(0))... Make that that this new script is only attached to one GameObject. Just attach it to an empty GameObject. That's it.

It will prevent problems such as executing code twice when there is an input event.


Now, let;s say that you want to notify another script attached to another GameObject when key is pressed, you can find that GameObject, get that script that is attached to it then call its function.

GameObject obj = GameObject.Find("YourOtherObjectName");
Script script = obj.GetComponent<Script >();
script.notifyKeyPress();

I attached it to multiple game objects to be able to detect which game object was clicked

That's not how to detect which GameObject is clicked. Since this is a plane, I assume it is a 3D Object with a 3D collider. See 6.For 3D Object (Mesh Renderer/any 3D Collider) from my other post on how to do this.

Programmer
  • 121,791
  • 22
  • 236
  • 328
  • I attached it to multiple game objects to be able to detect which game object was clicked - I want to create my prefab in its center. Your answer however reminded me of one important thing - I should check if I clicked on `this` object :) . I will accept it however, because it answers the question I asked :). – Marek M. Jul 20 '17 at 22:21
  • It's a plane with a kind of a grid on it - I'm just trying to detect which square was clicked. – Marek M. Jul 20 '17 at 22:25
  • See the link from the edited answer. You don't have to ask a new question for that since I've already answered that before. – Programmer Jul 20 '17 at 22:26
  • I don't know how to make your code (from the other question) work... I've added a box collider to each tile on my plane and attached the script to main camera and when it didn't work - to an empty game object (with `EventSystem` attached). No result so far... – Marek M. Jul 21 '17 at 08:01
  • Create a new question for that and include your code for that. I will quickly take a look at it. Also mention what type of collider you are using in that question. – Programmer Jul 21 '17 at 08:03
  • I've just created this question: https://stackoverflow.com/questions/45232431/how-to-detect-which-object-was-clicked-in-unity3d – Marek M. Jul 21 '17 at 08:11