Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved If statement not working.

Discussion in 'Scripting' started by DualOnGames, May 10, 2022.

  1. DualOnGames

    DualOnGames

    Joined:
    Jan 20, 2022
    Posts:
    33
    This if statement does not trigger, even if the "triggerer" variable is null.
    (The if statement above it does work.)
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Enemy : MonoBehaviour{
    6.     // Start is called before the first frame update
    7.  
    8.     public GameObject target;
    9.  
    10.     void Start(){
    11.  
    12.     }
    13.  
    14.     // Update is called once per frame
    15.     void Update(){
    16.         if(gameObject.transform.Find("Radius").GetComponent<RadiusScript>().triggerer != null){
    17.             print("triggerer is not null");
    18.         if(gameObject.transform.Find("Radius").GetComponent<RadiusScript>().triggerer.CompareTag("P")){
    19.             target = gameObject.transform.Find("Radius").GetComponent<RadiusScript>().triggerer;
    20.             print("triggerer tag returned true; target set.");
    21.         }
    22. // this if statement right below doesn't work.
    23.         if(gameObject.transform.Find("Radius").GetComponent<RadiusScript>().triggerer == null){
    24.             target = null;
    25.             print("target data cleared");
    26.         }
    27.         }
    28.         if(target == null){
    29.         if(Random.Range(0,100) == 0){
    30.             StartCoroutine(move(0.1f,0));
    31.         }else if(Random.Range(0,100) == 1){
    32.             StartCoroutine(move(0,0.1f));
    33.         }else if(Random.Range(0,100) == 2){
    34.             StartCoroutine(move(-0.1f,0));
    35.         }else if(Random.Range(0,100) == 3){
    36.             StartCoroutine(move(0,-0.1f));
    37.         }
    38.         }
    39.     }
    40.  
    41.  
    42.     IEnumerator move(float x, float z){
    43.         for(int i = 0; i < Random.Range(10,100); i++){
    44.         transform.Translate(x,0,z);
    45.         yield return new WaitForSeconds(Random.Range(0.01f,0.05f));
    46.         }
    47.     }
    48. }
    49.  
    Also, I have debugged a bit and found that, despite the fact that "triggerer" is null when it should be suggests that the if statement is not working for other reasons than incorrect data.
     
    Last edited: May 10, 2022
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Wow, my brain exploded trying to parse those lines. Does the above code even compile?! And for goodness sake, fix the indentation... lines 28 and 29 are just misleading af.

    If you have more than one or two dots (.) in a single statement, you're just being mean to yourself.

    How to break down hairy lines of code:

    http://plbm.com/?p=248

    Break it up, practice social distancing in your code, one thing per line please.
     
    TaleOf4Gamers and angrypenguin like this.
  3. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Just to show you what I mean, here's the indented code:

    Screen Shot 2022-05-10 at 10.46.39 AM.png


    Since you check for it to NOT be null at the top, you should certainly never expect it to BE null inside of the
    if
    statement block, the
    if
    that you flag as problematic.

    Also, seriously, cache the RadiusScript at the start... the above code is just compiler abuse!
     
  4. Peeling

    Peeling

    Joined:
    Nov 10, 2013
    Posts:
    442
    If you indent your code properly, you'll see why it isn't working. The 'non-working' if is inside an if NOT null.
     
  5. DualOnGames

    DualOnGames

    Joined:
    Jan 20, 2022
    Posts:
    33
    Sorry, I'm kind-of a messy programmer. Also I have no idea how i did not spot that issue.
    thanks.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    Yeah, me too, but some structure is critical. All that indentation stuff should be automatic from visual studio . Is it not doing so??

    Try press Ctrl-I to format your sourcecode properly.

    This may help you with intellisense and possibly other Visual Studio integration problems:

    Sometimes the fix is as simple as doing Assets -> Open C# Project from Unity. Other times it requires more.

    Other times it requires you also nuke the userprefs and .vsconfig and other crufty low-value high-hassle files that Visual Studio tends to slowly damage over time, then try the above trick.

    Barring all that, move on to other ideas:

    https://forum.unity.com/threads/intellisense-not-working-with-visual-studio-fix.836599/

    Also, try update the VSCode package inside of Unity: Window -> Package Manager -> Search for Visual Studio Code Editor -> Press the Update button

    Also, this: https://forum.unity.com/threads/no-suggestions-in-vscode.955197/#post-6227874
     
  7. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,616
    I think you have a pretty good idea. ;)

    No worries, everyone starts somewhere. Time to change that one, though.

    Consider: In this case it took you well over an hour to get a solution via a forum post, where you could have spent less than a minute and it'd have become obvious on its own.

    Coders are pretty picky when it comes to use of white space, and it's not because we all happen to have the same random obsession. It's because counting braces is time consuming and error prone, and - quite frankly - silly, because if we lay out our code properly there's no need to ever do it.

    And once you're in the habit of formatting your code as you go it'll basically be free.*

    Programming is hard enough without making it harder for ourselves. :)

    * Tip: in many code editors you can select multiple lines at a time and press tab or shift-tab to (un)indent them all together. Also see what Kurt-Dekker says about using auto-format.
     
    Kurt-Dekker likes this.
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,520
    I am going to add the above quote to my "hairy code" blurb, with attribution to you of course.
     
    Bunny83 and angrypenguin like this.
  9. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,616
    Go for it. No idea if it's actually mine, though, or a variation on something I heard before.
     
    Kurt-Dekker likes this.
  10. DualOnGames

    DualOnGames

    Joined:
    Jan 20, 2022
    Posts:
    33
    Funny thing, Actually. The night before I had posted this thread I found this issue but ran out of time.
    the next day, I forgot what the issue was and for some reason, I just didn't see it.