Search Unity

Select/Deselect Script

Discussion in 'Scripting' started by sdwsk, Sep 18, 2018.

  1. sdwsk

    sdwsk

    Joined:
    Aug 28, 2018
    Posts:
    34
    Hi guys!
    I am creating a game in which you have to click building, UI element will show up and stuff, and when this 'building' is selected and anything but UI element is clicked(small box with buttons and statistics, it's not the case tho), the building got deselected (inside it's script value isSelected is changed to false). And if you click other building, the first one becomes deselected and the new one selected (I achieved this by making prevBuilding variable and filling this value with activeBuilding value as a first step of building selection method)
    But I have two problems here:
    1. On first click in-game none of the buildings gets selected (because prevBuilding value is empty I think)
    and 2. Everything works fine as long as I don't click something different than the 'building' game object. When I click let's say ground, two buildings are selected and I can't deselect any of them.
    Here's my script
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. public class MouseSelection : MonoBehaviour {
    5.  
    6.     private GameObject previousBuilding;
    7.     private GameObject activeBuilding;
    8.  
    9.     private void Update()
    10.     {
    11.         if (Input.GetKeyDown("mouse 0"))
    12.         {
    13.             RaycastHit selectionRaycastHit;
    14.             Ray selectionRay = Camera.main.ScreenPointToRay(Input.mousePosition);
    15.             if (Physics.Raycast(selectionRay, out selectionRaycastHit, 100f))
    16.             {
    17.                 previousBuilding = activeBuilding;
    18.                 Debug.Log("it works!");
    19.                 Selection selection = selectionRaycastHit.collider.GetComponent<Selection>();
    20.                 activeBuilding = selectionRaycastHit.collider.gameObject;
    21.                 if (selection != null)
    22.                 {
    23.                     if (selection.isSelected == false)
    24.                         OnBuildingSelect(selection);
    25.                     //else
    26.                         //OnBuildingDeselect(selection);
    27.                 }
    28.             }
    29.         }
    30.     }
    31.     private void OnBuildingSelect(Selection sel)
    32.     {
    33.         Debug.Log("select");
    34.         Selection prevSel = previousBuilding.GetComponent<Selection>();
    35.         prevSel.isSelected = false;
    36.         sel.isSelected = true;
    37.     }
    38.     private void OnBuildingDeselect(Selection sel)
    39.     {
    40.         Debug.Log("deselect");
    41.         sel.isSelected = false;
    42.         activeBuilding = null;
    43.     }
    44. }
    Btw Selection class contains only simple script that checks if isSelected is true then the cube changes material (for playtesting purposes).

    Thanks for help,
    Siudinho.