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

I am not sure why my third if statement is not working

Discussion in 'Scripting' started by SuperCrow2, Oct 23, 2021.

  1. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    EDIT: IT'S NOT THE THIRD IF THAT WASN'T WORKING, IT WAS THE SECOND "&&" THAT I INCLUDED THAT CAUSED THE SCRIPT TO NO LONGER WORK

    The land would no longer develop once I added the second "&&" but would develop once I commented out the second "&&" and kept the third if statement enabled.





    Code (CSharp):
    1.     //IF RAY HITS ROAD AND POWER POLE AND WATER PIPE, LAND IS DEVELOPED
    2.        
    3.             if (hitInfo.collider.CompareTag("RoadTag") && hitInfo2.collider.CompareTag("PowerPoleTag") && hitInfo3.collider.CompareTag("WaterPipeTag"))
    4.             {
    5.  
    6.            
    7.                 // Check if ray from the un-developed land is hitting a collider (road collider, power pole collider)
    8.          
    9.                 //ROAD
    10.                 if (hitInfo.collider != null)  //The inequality operator != returns true if its operands are not equal, false otherwise.
    11.  
    12.                 {
    13.                     //POWER POLE
    14.                     if (hitInfo2.collider != null)
    15.                     {
    16.  
    17.                         //WATER PIPE
    18.                         if (hitInfo3.collider != null)
    19.                    
    20.                             Debug.Log("ray hit road"); //this debug is not being read
    21.                             hitCount++;
    22.  
    23.                             StartCoroutine(StartDelay()); //wait 7 seconds for land to develop.
    24.                             Debug.Log("Land Developed");
    25.  
    26.                         }
     
    Last edited: Oct 23, 2021
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
    At line 9 (or even before line 3!) print all 3 colliders out, see what they are.

    Also, because of the overloaded null check thingy in UnityEngine.Object you can shortcut those statements to simply:

    Code (csharp):
    1. if (hitInfo.collider)
    2. {
    3. ....
     
    Last edited: Oct 23, 2021
    matzomat likes this.
  3. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Those are the colliders on each of my three objects with their respective tag names
     
  4. SuperCrow2

    SuperCrow2

    Joined:
    Mar 8, 2018
    Posts:
    584
    Oops sorry I made my thread wrong. The land would no longer develop once I added the second "&&" but would develop once I commented out the second "&&" and kept the third if statement enabled.