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

Question Raycasts and lists removal

Discussion in 'Scripting' started by mathieu548, Jul 9, 2021.

  1. mathieu548

    mathieu548

    Joined:
    Jan 21, 2014
    Posts:
    10
    Hello there,

    I'm trying to create the following method to figure out whether cards are adjacent to each other or not.
    The thing is the cards get added in the adjacents' list but never removed even with the code in the down case which will of course be replicated in the other cases, any clue ? I'd like not to use colliders please.

    Code (CSharp):
    1.  
    2. public class ACard : MonoBehaviour
    3. {
    4.     //Adjacence
    5.     public bool hit_right;
    6.     public bool hit_left;
    7.     public bool hit_up;
    8.     public bool hit_down;
    9.  
    10.     public List<ACard> adjacents = new List<ACard>();
    11.  
    12.     public int counter_right = 0;
    13.     public int counter_left = 0;
    14.     public int counter_up = 0;
    15.     public int counter_down = 0;
    16.  
    17.     //variable test
    18.     private ACard right;
    19.     private ACard left;
    20.     private ACard up;
    21.     private ACard down;
    22.  
    23.     private void Update()
    24.     {
    25.         UpdateAdjacenceRaycast();
    26.     }
    27.     public void UpdateAdjacenceRaycast() // ajout ok retrait pas encore
    28.     {
    29.         hit_right = Physics.Raycast(transform.position, Vector3.right, out RaycastHit right_info, raycastDistance, mask);
    30.         hit_left = Physics.Raycast(transform.position, Vector3.left, out RaycastHit left_info, raycastDistance, mask);
    31.         hit_up = Physics.Raycast(transform.position, Vector3.up, out RaycastHit up_info, raycastDistance, mask);
    32.         hit_down = Physics.Raycast(transform.position, Vector3.down, out RaycastHit down_info, raycastDistance, mask);
    33.  
    34.         if (hit_right)
    35.         {
    36.             right = right_info.transform.GetComponent<ACard>();
    37.             if (!adjacents.Contains(right))
    38.             {
    39.                 adjacents.Add(right);
    40.             }
    41.         }
    42.         if (hit_left)
    43.         {
    44.             left = left_info.transform.GetComponent<ACard>();
    45.             if (!adjacents.Contains(left))
    46.             {
    47.                 adjacents.Add(left);
    48.             }
    49.         }
    50.         if (hit_up)
    51.         {
    52.             up = up_info.transform.GetComponent<ACard>();
    53.             if (!adjacents.Contains(up))
    54.             {
    55.                 adjacents.Add(up);
    56.             }
    57.         }
    58.         if (hit_down)
    59.         {
    60.             down = down_info.transform.GetComponent<ACard>();
    61.             Debug.Log(down);
    62.             if (!adjacents.Contains(down))
    63.             {
    64.                 adjacents.Add(down);
    65.             }
    66.             if (down==null)
    67.             {
    68.                 adjacents.Remove(down);
    69.                 Destroy(down);
    70.             }
    71.  
    72.         }
    73.  
    74.         Debug.DrawRay(transform.position, Vector3.right * raycastDistance, Color.red);
    75.         Debug.DrawRay(transform.position, Vector3.left * raycastDistance, Color.blue);
    76.         Debug.DrawRay(transform.position, Vector3.up * raycastDistance, Color.yellow);
    77.         Debug.DrawRay(transform.position, Vector3.down * raycastDistance, Color.green);
    78.     }
    79. }
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

    Doing this should help you answer these types of questions:

    - is this code even running? which parts are running? how often does it run? what order does it run in?
    - what are the values of the variables involved? Are they initialized? Are the values reasonable?

    Knowing this information will help you reason about the behavior you are seeing.

    You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

    You could also just display various important quantities in UI Text elements to watch them change as you play the game.

    If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

    Here's an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

    https://forum.unity.com/threads/coroutine-missing-hint-and-error.1103197/#post-7100494
     
    mopthrow likes this.
  3. mathieu548

    mathieu548

    Joined:
    Jan 21, 2014
    Posts:
    10
    I did but don't see what's wrong
     
  4. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,762
    Strip it down to its simplest form: two cards that meet the condition you think for removal.

    NOW... ask yourself some questions, answering them by putting in Debug.Log()

    - is ANY of the code you think running actually running?
    - is that condition being detected?
    - is the card being removed?
    - is something else happening?
    - any errors?

    etc
     
  5. mopthrow

    mopthrow

    Joined:
    May 8, 2020
    Posts:
    343
    The if condition on 58 is only true if you hit something with your hit_down raycast.

    Inside that code block, you only remove on 68 if your down is null, i.e. you didn't hit something.

    You can't hit something and not hit something at the same time ;)
     
    mathieu548 likes this.