- Home /
[C#] Physics.Raycast returns an array and can't access the length
Ok I've been stuck on this for hours. For some strange reason I cannot for the life of me access the length of an array with the given example in the scripting reference.
here is the snippet of the code.
public override bool ShootAt(Vector3 location)
{
switch( ProjectilePrefab.Type )
{
case EProjectileType.Instant:
{
if( !CanFire( ) ) return( false );
FireRateCooldown = FireRate;
CurrentAmmo -= AmmoConsumption;
int[] testArray = new int[5];
RaycastHit[] test = new RaycastHit[20];
Ray ray = GenerateRaycast();
RaycastHit[] hits;
hits = Physics.RaycastAll(ray.origin, ray.direction, ProjectilePrefab.Range);
for( int i = 0; i < hits.length; i++)
{
//blah blah blah
}
}
for some reason unknown to me I get this compiler error: Assets/source/proto_code/PrimaryWeapon.cs(109,66): error CS1061: Type UnityEngine.RaycastHit[]' does not contain a definition for
length' and no extension method length' of type
UnityEngine.RaycastHit[]' could be found (are you missing a using directive or an assembly reference?)
Answer by Ziibii · Mar 12, 2013 at 05:18 AM
Well not really answer to why hits.length isn't working. It should, but can't you just use?
foreach(var hit in hits) { hit.DoSomething(); }
or if you need index of currently processed hit
int i = 0; foreach(var hit in hits) { hit.DoSomething(); i++; }
Answer by Tarlius · Mar 12, 2013 at 04:09 AM
Its Array.Length. Note the capital L.
Wow. I feel like a complete idiot! Thank you for clearing that up for me.