Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Input.GetMouseButtonDown propagates to all the prefabs in my game, instead of just one.

Discussion in 'Input System' started by rosco_y, Nov 15, 2022.

  1. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    I have an array of prefabs in my project. Each prefab is a single cube with various components:
    upload_2022-11-15_14-58-35.png

    The constructed array looks like:
    CUBE_ARRAY.jpg

    My "preCube" prefab has a script component with an Update Event which reads like
    (Slightly simplified -- it contains two lines of code that set them looking at the Camera, and
    set their rotations to the Camera's rotation.):

    private void Update()
    {
    if (Input.GetMouseButton(0))
    UserValue = g.UserValue;
    if (Input.GetMouseButton(1))
    {
    if (!GetCandidate(-SudoValue))
    SetCandidate(-SudoValue, true);
    else
    SetCandidate(-SudoValue, false);
    }
    }

    My problem is that I'd written the Update() event hoping that it would act on a single prefab when it was clicked on, but in fact, when I click anywhere on my game view, the Update() event on every Cube in my array gets called and all my UserValues are set as though I'd left-clicked on each Cube.

    The Canvas components on the preCube are all sized very close to the size of the face of a single cube.

    Additionally, all of my Update input.OnMouseButtonDown() events fire even if I click anywhere outside of the array of cubes.

    Can anyone please tell me what I might be doing wrong?
    Thanks!
     
    Last edited: Nov 19, 2022
  2. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    Additionally it might be helpful to show an image of my cube array after I click on 1 cube. I have written code that should reveals the value (in blue) , of the cube that is clicked on, but as I was trying to say, it appears as though I've clicked on every single cube in the array:

    sudoValuesPropagatedScaled.jpg

    Again, thank you for your help!
     
  3. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    Here is an important detail that I've just discovered: All the cubes recieve the Input.GetMouseButton(n) event in the cube.Update() event when I click anywhere in the game (i.e., when I click in the empty space surrounding my array of cubes.)
    This is killing me!
     
  4. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    So I simplified my problem by making my cube array a lot smaller:

    upload_2022-11-19_10-37-46.png

    Each cube has a script component with an Update() event:

    private void Update()
    {
    if (Input.GetMouseButtonUp(0))
    {
    Debug.Log(ID);
    }
    }

    This Console output was generated when I clicked in "outer space" 10 times. (not clicking directly on any cube, or any other object.)

    upload_2022-11-19_10-43-41.png

    So I'm convinced that clicking anywhere in my game triggers the Input.GetMouseButtonUp() event in each cube's Update event.

    Help, I need your insight!
    Ross.
     
  5. rosco_y

    rosco_y

    Joined:
    Aug 3, 2016
    Posts:
    31
    SOLVED:

    Or at least I've achieved what I need using a different method.

    Instead of using the Update() event and OnMouseButtonXX() methods, I placed a box collider on each of the cubes and am using OnMouseUp(). Now I can click on each cube indiidually, although I'm missing my LEFT/RIGHT mouse button seperation, which feels like another problem.

    If anyone has a solution for that--I'd be glad to hear about it!

    Thanks Again,

    Ross.