Search Unity

Mouse Click Handling when placing an object.

Discussion in 'Scripting' started by Zefurion, Jun 23, 2019.

  1. Zefurion

    Zefurion

    Joined:
    Apr 12, 2019
    Posts:
    4
    Good evening. I am currently trying to create a game that can be thought of as an RTS for simplicity sake.

    My current problem is that when i place an object on the terrain it activates the side tools that are associated with the game object on the same left click that is used to place down the object.

    To be clear, i am placing down a Stockpile game object, and on the same left click that is used to place it down, the sidebar opens up with all the relevant stock pile information such as the number of resources present.

    I have already tried changing the tags and layers, but still it executes on the same frame.

    Could anyone more experienced than me help me figure out how to handle this? Thank your so much!
     
  2. DungeonBrickStudios

    DungeonBrickStudios

    Joined:
    Jun 9, 2015
    Posts:
    69
    You could have a bool value that is true only when placement is complete, then check for it when clicking for info.

    Also note that you have methods for getting the one frame the click began, all frames the click is held down, and the one frame the click is released. These are Input.GetMouseButtonDown, GetMouseButton and GetMouseButtonUp.
     
  3. Zefurion

    Zefurion

    Joined:
    Apr 12, 2019
    Posts:
    4
    OK, so with your suggestion DBS, i pretty much solved the problem, but i would like to know if its an efficient way.

    I Placed a boolean isSelectable in the game object.

    This is actually an interface method that i intend to use on all selectable objects.

    I then created a second interface method called SetIsSelectable which flips to the boolean to true, however this was still inadequate because the same problem persisted.

    The way i solved this is to add a WaitForSeconds Coroutine which after the object is placed, it waits 0.1f seconds before flipping the boolean isSelectable to true;

    During all testing it seems to work well...

    And literally as i am writing this i find the TRUE culprit of my problems...

    I had the script that handles my mouse clicks set to GetMouseButton instead of GetMouseButtonDown...so no wonder it wasn't working...

    Thanks for your help.
     
  4. DungeonBrickStudios

    DungeonBrickStudios

    Joined:
    Jun 9, 2015
    Posts:
    69
    Awesome, no problem. You probably already did this, but if the solution works without a coroutine, you could get rid of the coroutine.