- Home /
GetComponent() failing for enemy colliders from OverlapSphereNonAlloc array, but working fine from Raycast output
Hi, I'm educated in CS, but I'm a beginner in Unity. Currently modifying the tutorial survival shooter by adding grenades.
Pulling my hair out a bit on this one, since the debugger shows the same gameobjects and colliders in both cases, but my GetComponent() call only returns null in the OverlapSphereNonAlloc case.
This Shoot() script correctly gets the EnemyHealth component from enemy colliders hit by Raycast, and applies damage correctly:
Whereas this Explosion() script correctly identifies enemy colliders in range, but gets only nulls from the GetComponent() calls:
To my eye, these calls are the same in terms of class/hierarchy relationships... so I can't explain the failures in the grenade script. Unity version = 2020.2.1f1.
Would be really grateful for a pointer or two, so I can get back to building and learning! Thanks.
Answer by logicandchaos · Jan 10, 2021 at 12:50 PM
Line 71 try m_ExplosionHitCache[i].collision or collider.. I feel you are not pointing to the right object.
Answer by Mercuri · Jan 10, 2021 at 07:33 PM
Thanks for the suggestion.
I double checked that the third param of OverlapSphereNonAlloc() is of type static Collider[]. Also tried modifying to insert the .collider on line 71 and got this error indicating I was already on the collider component of the GameObject: "Assets_Complete-Game\Scripts\Weapons\GrenadierGrenade.cs(71,34): error CS0619: 'Component.collider' is obsolete: 'Property collider has been deprecated. Use GetComponent() instead. (UnityUpgradable)'
Just to be sure, I also went ahead and changed line 71 to EnemyHealth eh = m_ExplosionHitCache[i].GetComponent ().GetComponent ();
But there's no change to behavior, or to the m_ExplosionHitCache[i] objects when debugging
BTW - apologies for the poor formatting. I'm using carriage returns and the "code sample" button in the toolbar here, but the formatting seems to be lost/stripped when I post.