Making an Array of Rays

Hi all,

I’m trying to perform a Raycast scan around an object with a set granularity (i.e. 100 rays per 360 degrees) and read all of the Rays I generate that do not hit something and those that do into separate Arrays. However, the code I have written comes up with an ‘object not set to reference of an object’ and I can’t see why.

Just for the record, I’d rather use arrays than lists for this since I want this to work as quickly and efficiently as possible, but if lists work I won’t complain!

Any thoughts would be really appreciated!

Cheers,

Popuppirate

	public Ray[] free_rays;
	public Ray[] hit_rays;
	public RaycastHit[] hit_ray_info;
	public int granularity;
	public float step;
	public float safe_distance;

	// Use this for initialization
	void Start () {

		safe_distance = 3f;
		granularity = 100;
		step = 0;
	
	}
	
	// Update is called once per frame
	void Update () {

		                               
		if (step <= 360f) {
			for (int i=0; i<granularity; i++) {
				RaycastHit hit;
				Vector3 scan = Quaternion.AngleAxis (step, transform.forward) * transform.forward;
				Ray ray = new Ray (transform.position, scan);
			
				if (Physics.Raycast (ray, out hit, safe_distance)) {
				hit_rays  *= ray;*

hit_ray_info = hit;
* } else {*
free_rays = ray; //ERROR HERE
* }*
_ step +=360f / granularity * 1.0f;
* }
}else {
step =0f;
}*_

I would guess the arrays are not set to any size.

Though List is pretty much as fast as Array that you would not consider them in this case. Particularly since you are making a mess of your arrays. You place the hit in the arrays based on the value of i. How do you know where is the next one in this case? I give you a clue, you don’t.

This is what it is about to look like:

Index    ArrayHit    ArrayNoHit
0        Hit         null
1        Hit         null
2        null        NoHit
3        Hit         null
4        null        NoHit
5        Hit         null

With a list you would add as they come and one list is full of hit and the other full of no hit and no space between each of them.

And this is what it is about to look like:

Index    ListHit     ListNoHit
0        Hit         NoHit
1        Hit         NoHit
2        Hit        
3        Hit