Search Unity

Selecting GameObject by mouse click

Discussion in '2D' started by LightBubbleGum, Nov 20, 2018.

  1. LightBubbleGum

    LightBubbleGum

    Joined:
    Nov 19, 2018
    Posts:
    1
    Hello every one,
    Not so soon I started stading the Unity and was faced with problem that GameObject could not selected by mouse click (full description below). Does any body had the same problem?

    In my project GameObjects were created as a 2D object -> simple sprite and with sprite option like a Knob (circle) with size (x = 5, y = 5, z = 0).
    Based on random number In function Start the clones of this object are creating.
    I already create the script which is used for rectangle selection (like in strategy game).
    Code (CSharp):
    1. Vector3 mousePosition1;
    2.  
    3. //[...]
    4.  
    5. void Update()
    6. {
    7.         if (Input.GetMouseButtonDown(0))
    8.         {
    9.             isSelecting = true;
    10.             mousePosition1 = Input.mousePosition;
    11.             isReleased = true;
    12.         }
    13.         // If we let go of the left mouse button, end selection
    14.         if (Input.GetMouseButtonUp(0))
    15.         {
    16.                 for (int i = 0; i < allPlaces.Count; ++i)
    17.                 {
    18.                     var go = allPlaces[i];
    19.                     if (IsWithinSelectionBounds(go))
    20.                     {
    21.                         if (go.gameObject.tag == "UnitSpace")
    22.                         {
    23.                             selectedObjects.Add(go);
    24.                             isSelected = true;
    25.                         }
    26.                     }
    27.             }
    28.             isSelecting = false;
    29.         }
    30. }
    Please do not get in stuck on code stile it was just for test.
    Any way this sript is working and objects added in List.

    But common way for single selection (which I have found in internet) does not wrok.
    I am talking about this:
    Code (CSharp):
    1.             RaycastHit hit;
    2.             if (Physics.Raycast(Camera.main.ScreenPointToRay(Input.mousePosition), out hit, 200.0f))
    3.             {
    4.                 Debug.Log(hit.collider.tag);
    5.                 if (hit.collider.tag == "UnitSpace")
    6.                 {
    7.                     GameObject activeUnit = hit.collider.gameObject;
    8.                     //Add it to the list of selected units, which is now just 1 unit
    9.                     selectedObjects.Add(activeUnit);
    10.                     isSelected = true;
    11.                 }
    12.             }
    13.  
    How much would I press, there was not situation when in debug log I have seen any information. It means that script didn't pass throw the "if" condition.

    So my question is: Does anybody know how to select object by clicking the left button on a mouse?
    Sorry for long description and if I have missed something.
     
  2. vakabaka

    vakabaka

    Joined:
    Jul 21, 2014
    Posts:
    1,153
    this will not work, 3d and 2d physic don't works together

    try:
    RaycastHit2D hit;
    if (Physics2D.Raycast(Camera.main.....
    and use collider2d
     
    S1NATB, BioAbner and ubuntudiego like this.