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) ?

6
  • 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 Commented Jul 20, 2017 at 21:50
  • But I'm not holding down the mouse button, I even tried with other mouse - the same effect. Commented Jul 20, 2017 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 Commented Jul 20, 2017 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). Commented Jul 20, 2017 at 22:02
  • Is ClickSymbol a prefab? If yes, is the PlaneBehaviour script attached to this ClickSymbol prefab or any of its child? Commented Jul 20, 2017 at 22:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

6 Comments

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 :).
It's a plane with a kind of a grid on it - I'm just trying to detect which square was clicked.
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.
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...
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.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.