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

OnDeselect is not working

Discussion in 'Scripting' started by jsmall81, Jan 31, 2021.

  1. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Alright, try to ignore my tons of logs... I can't figure out why my UI isn't deselecting... It will run the Deselect method and print out that it runs, but it's not actually disabling my gameObject which is just a UI. It will enable it, just not disable it. Did I do something wrong? Basically, if I click my terrain, something without my "Building" script or don't have authority over an object, it is supposed to disable my UI. It's setup in the editor with the Selected gameObject bool checked on and off for each respective choice.

    Code (CSharp):
    1.     [Client]
    2.     private void SelectBuilding()
    3.     {
    4.         Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
    5.  
    6.         if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, layerMask))
    7.         { return; }
    8.  
    9.         if (!hit.collider.TryGetComponent<Building>(out Building building))
    10.         {Deselect(); print ("BUILDING: SelectBuilding() No Building component"); return; }
    11.  
    12.         if (!building.hasAuthority)
    13.         {print ("BUILDING: SelectBuilding() No auth over building"); Deselect(); return;}
    14.  
    15.         if (!building.hasBuildMenu == true)
    16.         {print ("BUILDING: SelectBuilding() No Build menu"); Deselect(); return;}
    17.  
    18.         print ("BUILDING: SelectBuilding() Building Selected");
    19.         Select();
    20.     }
    21.  
    22.     [Client]
    23.     private void DeSelectBuilding()
    24.     {
    25.         Ray ray = mainCamera.ScreenPointToRay(Mouse.current.position.ReadValue());
    26.  
    27.         if (!Physics.Raycast(ray, out RaycastHit hit, Mathf.Infinity, floorMask))
    28.         {print("BUILDING: DeSelectBuilding() Raycast did not hit Layermask"); Deselect(); return; }
    29.  
    30.         if (!hit.collider.TryGetComponent<Building>(out Building building))
    31.         {print ("BUILDING: DeSelectBuilding() Raycast did not hit Building component"); Deselect(); return; }
    32.  
    33.         print ("BUILDING: DeSelectBuilding() Building DeSelected");
    34.         Deselect();
    35.  
    36.     }
    37.  
    38.     [Client]
    39.     public void Select()
    40.     {
    41.         //if (!hasAuthority) {print("BUILDING: SELECT: No auth over Building."); return; }
    42.         print("BUILDING: Select() Running ONSELECT");
    43.         onSelected?.Invoke();
    44.     }
    45.  
    46.     [Client]
    47.     public void Deselect()
    48.     {
    49.         print("BUILDING: Deselect() Running DESELECT");
    50.         onDeselected?.Invoke();
    51.     }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Did you hook up a listener to the onDeselected event?
     
  3. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Do you mean in the editor? I have my UI object in the Onselect and OnDeselect in the editor. Check mark on for selec, off for deselect.
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Logs are good, logs are excellent... the only thing I see you NOT logging is if the most important thing of all, the final step!

    How would you know if
    onSelected
    or
    onDeselected
    delegates are null? You use that
    ?
    operator and it will quietly do nothing when those are null.

    And hey, look... you're seeing nothing happen!

    I'd log that first.
     
  5. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Sorry, I'm pretty new to programming in general. Basically, trial and error with everything. I thought I was logging it? With the log right before it. How would I log that exact function? Does it have to be on the same line?
     
  6. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    I don't really know what the? Is doing.
     
  7. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    The ? operator says "If there are no delegates here, don't invoke this and don't cause an error."

    Try removing the question mark... if you get runtime errors then you know those delegates were blank and need to be set (which is what Praetor asked above).

    Your log above it just prints a text, regardless.

    You could print something like check if that delegate is null, or check out many listeners it has (there's a method but I'm not sure which delegate type you're using, just check the docs for the one you are using).
     
    jsmall81 likes this.
  8. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    I removed the ? and it does not post any errors in the console when it does the "deselect". Still the same result. It doesn't disable my UI component that I have enable on "Select".
     
  9. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,723
    Show a screenshot of your onSelect event in the inspector.
     
  10. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Here it is. So, on selected, enable Building display, which is my build options thing. On deselected, disable it. Doesnt work for some reason.
     
  11. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    I'm just following some tutorials, trying to figure things out and learn along the way. The courses I'm following don't seem to be very good at actually teaching, though. Just "Copy what I do" and not teaching what things do. So, I don't know how to do what you're asking. I'm trying to research that now.
     
  12. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    One thing I've noticed in other posts, is how they have their ondeselect / onselect setup. With the
    Code (CSharp):
    1. ISelectHandler / IDeselectHandler
    . I don't have those, but they weren't mentioned in the course I was following.
    Code (CSharp):
    1. public class ExampleClass : MonoBehaviour, IDeselectHandler //This Interface is required to receive OnDeselect callbacks.
    2. {
    3.  
    4.     public void OnDeselect (BaseEventData data)
    5.     {
    6.         Debug.Log ("Deselected");
    7.     }
    8. }
    Do you think this has something to do with it? I wouldn't think so, since OnSelect is working as it should. Also, I don't think I have any delegates setup to use OnDeselect, other than what is shown above. Not referencing this class, anyways.
     
  13. jsmall81

    jsmall81

    Joined:
    Jan 25, 2016
    Posts:
    22
    Long story short, I'm doing it the right way, it just doesn't work on that I ject for some reason.