List out of Raycast2D[] ?

Hey Guys i try to write a function for my AI which will give me some information what got hit. ( its a sensor) I want to use “All” because i need all the data in Range here is my Code:

(both classes are on the same script)

[System.Serializable]
public class SawStruct
{
    [SerializeField]
    public List<Aspect> _Aspect;
    [SerializeField]
    public List<Vector2> _Position;
    [SerializeField]
    public List<Directions> _Direction;

    public SawStruct()
    {
        _Aspect = new List<Aspect>(0);
        _Position = new List<Vector2>(0);
        _Direction = new List<Directions>(0);
    }

    public SawStruct( int _Count)
    {
        _Aspect = new List<Aspect>(_Count);
        _Position = new List<Vector2>(_Count);
        _Direction = new List<Directions>(_Count);
    }
}
public class SeeSense : Sense {

    public float f_ViewDistance     = 5f;
    public int i_ViewDepth            = 2;
    public float f_Stepdistance     = 1f;
    public Vector2[] rayDirections = new Vector2[4];

    [SerializeField]
    public List<RaycastHit2D[]> hit = new List<RaycastHit2D[]>();
    private int i_directionCount    = 2;
    private int i_SenseDirections = 4;

     
    //Detect perspective field of view of the ai character
    public SawStruct[] DetectAspect(Vector2 _PositionDelta)
    {
        //Init the new struct wich you give back with the value of the rays you need
        SawStruct[] ReturnStruct = new SawStruct[((i_directionCount * i_ViewDepth * i_SenseDirections) + i_SenseDirections)];  
        hit = new List<RaycastHit2D[]>((i_directionCount * i_ViewDepth * i_SenseDirections) + i_SenseDirections);
      
        Debug.Log(" hit : " + hit.Count);
        for (int i=0; i < hit.Count; i++)
        {
            Debug.Log( " Counter " + i);
            hit[i] = new RaycastHit2D[6];
        }
        Debug.Log(" hit[0] : " + hit[0].Length);

        //Direction from current position to player Position
        for( int i = 0; i < i_SenseDirections; i++)
        {
            //first 4 directions ( Sense Direction )
            Debug.Log(" Iteration : " + i);
            hit[i] = Physics2D.RaycastAll((Vector2)transform.position + _PositionDelta, rayDirections[i], f_ViewDistance);
            for (int j = 0; j < hit[i].Length; j++)
            {
            if( hit[i][j].transform != null)
                if( hit[i][j] )
                {
                Aspect OtherAspect = hit[i][j].collider.GetComponent<Aspect>();
                if( OtherAspect != null)
                    {
                        ReturnStruct[i]._Aspect.Add(OtherAspect);
                        ReturnStruct[i]._Position.Add(hit[i][j].point);
                        ReturnStruct[i]._Direction.Add(ReturnDirection(hit[i][j].point, true));
                    }
                }
                else
                {
                    Debug.Log("Hit something witout Aspect: " + hit[i][j].transform.name + "  at position: " + hit[i][j].point);
                    ReturnStruct[i]._Aspect.Add(null);
                    ReturnStruct[i]._Position.Add(Vector2.zero);
                    ReturnStruct[i]._Direction.Add(ReturnDirection(hit[i][j].point, false));
                }
            }
        }

        return ReturnStruct;
    }

So my Problem is that the init List is always zero!
Debug values:
hit : 0
Counter : 0 ( then Null reference)
hit[0] : Null reference

I Hope i just have a small mistake in the init :confused:

Thanks in Advance^^

Several erros/missconceptions here.

  1. Lists : When calling new List( capacity ), capacity does not define the all mattering length of the list just like we are used from normal arrays. Instead, the capacity does just define what the starting size of the list is. You can add more or less items to such a list depending on your need, itll resize itself.

  2. Not quiet sure on this but you should never initialize a List with zero capacity. Use the default constructor (empty) instead.

  3. hit = new List<RaycastHit2D[ ]>((i_directionCount * i_ViewDepth * i_SenseDirections) + i_SenseDirections);
    If you wanted to set the capacity than its ok! Otherwise, read above.

  4. SawStruct[ ] ReturnStruct = new SawStruct[((i_directionCount * i_ViewDepth * i_SenseDirections) + i_SenseDirections)];
    Several things I can think of here.
    a) You thought that the math you’ve done for the length gets passed to the structs constructor? It doesnt, it just specifies the arrays length.
    b) You are specifing the array length right there with [((i_directionCount * i_ViewDepth * i_SenseDirections) + i_SenseDirections)]
    c) You are just initializing the array! You dont set any element in the array! So loop throught every index of the array and call new SawStruct();!

  5. Since you never call the constructors of the SawStruct’s in the array, the lists in them never get initialized.

1 Like

Ok found a solution i jused used 2d arrays dnk if it works i will post when i testet it out here is what i got so far:

[System.Serializable]
public struct SawStruct
{
    [SerializeField]
    public List<Aspect> SAspect;
    [SerializeField]
    public System.Collections.Generic.List<Vector2> SPosition;
    [SerializeField]
    public List<Directions> SDirection;

    /*
    public SawStruct()
    {
        SAspect = new List<Aspect>();
        SPosition = new List<Vector2>();
        SDirection = new List<Directions>();
    }
    */

    public SawStruct( int _Count)
    {
        SAspect = new List<Aspect>(_Count);
        SPosition = new List<Vector2>(_Count);
        SDirection = new List<Directions>(_Count);
    }
}
public class SeeSense : Sense {

    public float f_ViewDistance     = 5f;
    public int i_ViewDepth            = 2;
    public float f_Stepdistance     = 1f;
    public Vector2[] rayDirections = new Vector2[4];

    //[SerializeField]
    //public List<RaycastHit2D[]> hit = new List<RaycastHit2D[]>();
    private int i_directionCount    = 2;
    private int i_SenseDirections = 4;

    //Detect perspective field of view of the ai character
    public SawStruct[] DetectAspect(Vector2 _PositionDelta)
    {
        //Init the new struct wich you give back with the value of the rays you need
        SawStruct[] ReturnStruct = new SawStruct[((i_directionCount * i_ViewDepth * i_SenseDirections) + i_SenseDirections)];
        for (int t = 0; t< ReturnStruct.Length; t++)
        {
            ReturnStruct[t].SPosition = new List<Vector2>();
            ReturnStruct[t].SDirection = new List<Directions>();
            ReturnStruct[t].SAspect = new List<Aspect>();
        }
        //hit = new List<RaycastHit2D[]>((i_directionCount * i_ViewDepth * i_SenseDirections) + i_SenseDirections);
        RaycastHit2D[,] hit = new RaycastHit2D[(i_directionCount * i_ViewDepth * i_SenseDirections) + i_SenseDirections, 0];

        Debug.Log(" hit : " + hit.Length);
        for (int i=0; i < hit.Length; i++)
        {
            Debug.Log(" hit[0][] : " + hit.GetLength(0));
            Debug.Log(" hit[][0] : " + hit.GetLength(1));
            //Debug.Log(" hit[1] : " + hit[i,1].Length);
        }


        //Direction from current position to player Position
        for( int i = 0; i < i_SenseDirections; i++)
        {
            //first 4 directions ( Sense Direction )
            Debug.Log(" Iteration : " + i);
            RaycastHit2D[] temp = Physics2D.RaycastAll((Vector2)transform.position + _PositionDelta, rayDirections[I], f_ViewDistance);
            hit = new RaycastHit2D[(i_directionCount * i_ViewDepth * i_SenseDirections) + i_SenseDirections,temp.Length];

            for ( int k=0; k< temp.Length; k++)
            {
                hit[i,k] = temp[k];
            }

            for (int j = 0; j < hit.GetLength(1); j++)
            {
            if( hit[i,j].transform != null)
                if( hit[i,j] )
                {
                Aspect OtherAspect = hit[i,j].collider.GetComponent<Aspect>();
                if( OtherAspect != null)
                {
                    Debug.Log("Hit something with Aspect: " + hit[i,j].transform.name + "  at position: " + hit[i,j].point);
                        Debug.Log("Lets see the Struct " + ReturnStruct.Length + " " + i);
                        Debug.Log(" Aspect " + ReturnStruct[I].SAspect);
                    ReturnStruct[I].SAspect.Add(OtherAspect);
                    ReturnStruct[I].SPosition.Add(hit[i,j].point);
                    ReturnStruct[I].SDirection.Add(ReturnDirection(hit[i,j].point, true));
                }
                }
                else
                {
                    Debug.Log("Hit something witout Aspect: " + hit[i,j].transform.name + "  at position: " + hit[i,j].point);
                    ReturnStruct[I].SAspect.Add(null);
                    ReturnStruct[I].SPosition.Add(Vector2.zero);
                    ReturnStruct[I].SDirection.Add(ReturnDirection(hit[i,j].point, false));
                }
            }
        }

        return ReturnStruct;
    }

[/I][/I][/I][/I][/I][/I][/I][/I]

Thanks for your answer helped me a lot and this post as well:
http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

It works well thank your for your help! Here is a screenshot with the information my BOT gets :wink: