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

Resolved I am apparently misunderstanding how else works.

Discussion in 'Scripting' started by barrlin, Jul 24, 2023.

  1. barrlin

    barrlin

    Joined:
    Jun 29, 2021
    Posts:
    8
    Code (CSharp):
    1. if(Input.GetKeyUp(KeyCode.Mouse1))
    2.         {
    3.             //Debug.Log("Mouseclick");
    4.             Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    5.             RaycastHit hit;
    6.  
    7.             if(Physics.Raycast(ray, out hit, ClickRange, interactableObject))
    8.             {
    9.  
    10.                 Interactable interactable = hit.collider.GetComponent<Interactable>();
    11.                 NPCtest NPCtest = hit.collider.GetComponent<NPCtest>();
    12.                 enemyscript enemyscript = hit.collider.GetComponent<enemyscript>();
    13.  
    14.                 selectedTarget = hit.transform;
    15.  
    16.                 if(hit.collider.gameObject.tag == "Enemy")
    17.                 {
    18.                     if(selectedTarget != null)
    19.                     {
    20.                         DeselectTarget();
    21.                         if(enemyscript != null)
    22.                         {
    23.                             Enscript = hit.collider.gameObject.GetComponent<enemyscript>();
    24.                             SelectTarget();
    25.                         }
    26.                     }
    27.                 }
    28.                 else
    29.                 {
    30.                     DeselectTarget();
    31.                     Debug.Log("DEEE");
    32.                 }
    33. }
    Debug.Log("DEEE"); under else is not being called.

    I understand this to mean "If raycast does not hit an interactable object within the float defined as clickRange" then DeselectTarget and say DEEE." Clearly I'm wrong. Would someone like to correct my thinking so I can get on to the other 5000 problems I'm having?

    The if statement works as intended.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,718
    That
    else
    is connected to this if statement on line 16:
    if(hit.collider.gameObject.tag == "Enemy")


    So here's how your logic works:

    If your raycast hits any object:
    - if that object has the "Enemy" tag, do the stuff between lines 18-26
    - otherwise (it has some other tag), Deselect the target and print "DEEE"

    You currently don't have any code handling the case if the raycast doesn't hit anything at all. if you wanted to do that, you'd make an else statement connected to the outer if statement (the one on line 7)
     
    Bunny83, Yoreki and barrlin like this.
  3. zulo3d

    zulo3d

    Joined:
    Feb 18, 2023
    Posts:
    510
    Code (CSharp):
    1.             if(Input.GetKeyUp(KeyCode.Mouse1))
    2.             {
    3.                 //Debug.Log("Mouseclick");
    4.                 Ray ray = cam.ScreenPointToRay(Input.mousePosition);
    5.                 RaycastHit hit;
    6.    
    7.                 if(Physics.Raycast(ray, out hit, ClickRange, interactableObject))
    8.                 {
    9.                     Interactable interactable = hit.collider.GetComponent<Interactable>();
    10.                     NPCtest NPCtest = hit.collider.GetComponent<NPCtest>();
    11.                     enemyscript enemyscript = hit.collider.GetComponent<enemyscript>();
    12.    
    13.                     selectedTarget = hit.transform;
    14.    
    15.                     if(hit.collider.gameObject.tag == "Enemy")
    16.                     {
    17.                         if(selectedTarget != null)
    18.                         {
    19.                             DeselectTarget();
    20.                             if(enemyscript != null)
    21.                             {
    22.                                 Enscript = hit.collider.gameObject.GetComponent<enemyscript>();
    23.                                 SelectTarget();
    24.                             }
    25.                         }
    26.                     }
    27.                     else
    28.                     {
    29.                         DeselectTarget();
    30.                         Debug.Log("DEEE");
    31.                     }
    32.                 }
    33.                 else
    34.                 {
    35.                     Debug.Log("Nothing clicked on");
    36.                 }
    37.             }
     
    barrlin likes this.
  4. barrlin

    barrlin

    Joined:
    Jun 29, 2021
    Posts:
    8
    Ah yes of course.... It's always the most obvious mistakes that confound me for hours.
     
    orionsyndrome and Yoreki like this.
  5. tleylan

    tleylan

    Joined:
    Jun 17, 2020
    Posts:
    521
    You have some potentially confusing coding conventions as well.

    You are naming NPCTest and enemyscript exactly the name of the class and the enemyscript class is lowercase.

    Also notice that you are assigning interactable, NPCtest, enemyscript and selectedTarget before you know if it is an "Enemy". You don't use those values unless it is an enemy so you can move them inside the if(hit) test. And it doesn't appear that NPCtest is used here.
     
    Bunny83, DragonCoder, barrlin and 2 others like this.
  6. barrlin

    barrlin

    Joined:
    Jun 29, 2021
    Posts:
    8
    It's not the full script but if you were to look at the entire thing you'd see a lot of unused and redundant lines (hence why I only post relevant parts lol). I usually keep clutter code until I know I definitely won't be using it, for reference or so I don't accidentally do the same thing twice. It might not be the best way of doing thing but it kinda works for me.

    In this case it was just the concept of the 'if and else' which I needed advice with so I didn't want to people waste their time digging through a few hundred lines of code written by a self learned noob.
     
  7. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,082
    You have a great deal of redundant indentation too. Since all of those if statements just skip the rest of the code if you swap the condition you end up with a smaller block and one less indentation which reduces the likelihood of errors. Line 16 is a little more complex thanks to combining two if statements but it's the same idea as the others.
    Code (csharp):
    1. if (Input.GetKeyUp(KeyCode.Mouse1))
    2. {
    3.     var ray = cam.ScreenPointToRay(Input.mousePosition);
    4.     RaycastHit hit;
    5.     if (!Physics.Raycast(ray, out hit, ClickRange, interactableObject))
    6.     {
    7.         Debug.Log("Nothing clicked on.");
    8.         return;
    9.     }
    10.  
    11.     var interactable = hit.collider.GetComponent<Interactable>();
    12.     var npcTest = hit.collider.GetComponent<NPCTest>();
    13.     var enemyScript = hit.collider.GetComponent<enemyscript>();
    14.  
    15.     selectedTarget = hit.transform;
    16.  
    17.     if (hit.collider.gameObject.tag != "Enemy" || selectedTarget == null)
    18.     {
    19.         DeselectTarget();
    20.         Debug.Log("DEEE");
    21.         return;
    22.     }
    23.  
    24.     DeselectTarget();
    25.     if (enemyScript == null) return;
    26.  
    27.     Enscript = hit.collider.gameObject.GetComponent<enemyscript>();
    28.     SelectTarget();
    29. }
     
    Last edited: Jul 24, 2023
    barrlin, Bunny83, DragonCoder and 2 others like this.
  8. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    If it helps, this will never really go away. I've been coding for 35-ish years. There are always ups and downs and it's super fun except when it's not.
     
    Ryiah and Kurt-Dekker like this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,563
    So true, so true. Anyone who says they have mastered realtime interactive entertainment software engineering is simply mistaken.

    It seems the main benefit to diligent study and application of self is that you minimize the amount of time you flop around like a dead fish wondering what on earth is wrong.

    This is why I am such a HUGE proponent of keeping it simple... simple simple simple simple, and then even simpler than that. One thing per line, ideally never indent deeper than you must, etc.

    It has to be so simple that you can explain it to a dog. See what I'm doing above in my avatar picture?

    Note the code rewrite that @Ryiah posted above. It is the CLASSIC linear simple example of top-down flow, with early-outs at line 8, line 21 and line 25.. so clean!

    As you read it you can immediately see where "We're done here!" happens, and all you do is move to the next line down the page if you're not done.

    It's like passing highway exits... once you're past it you know you have accomplished something and are now ready for the next step of the data cascade.

    Also of note is that attaching the debugger is SUPER easy here: just set a breakpoint right after an early out that you care about and your code will only break when the condition you actually care about happens.
     
    barrlin, Ryiah and orionsyndrome like this.