- Home /
The question is answered, right answer was accepted
Inconsistent Sphere Collider Behavior
Hello,
I'm making a game where there are enemies with their (sphere) collider disabled by default. When the player taps and drags on a character to command it to attack the enemy, I want the enemy collider to be enabled (for raycasting to this enemy target), and then disabled again when the player lifts his finger (OnMouseButtonUp). This is to prevent enemies from obstructing the main characters when selecting them. The code below works:
if (character_controller.didDrag && character_controller.whichChar != 1) //checks that the correct characters are chosen and player drags on screen
{
if (!transform.Find("raycastColl").GetComponent<SphereCollider>().enabled)
transform.Find("raycastColl").GetComponent<SphereCollider>().enabled = true;
}
else
{
if (transform.Find("raycastColl").GetComponent<SphereCollider>().enabled)
transform.Find("raycastColl").GetComponent<SphereCollider>().enabled = false;
}
Here are some problems:
1) I do not see the 'Sphere Collider' checkbox being triggered at all (appears to be always off). I assigned a dummy boolean to check in the inspector, and the dummy showed that it should be triggered:
2) Even though it appears to not be enabled, characters ARE able to target the enemies, provided the enemies are placed on the scene beforehand, which brings me to the next point;
3) When I instantiate enemies using a prefab, point 1) stands true, but for point 2), characters can NOT target the enemies;
These have me completely stumped. What makes it even more confusing is I have the exact same code on a boss enemy which works perfectly, with the collider triggered on and off (and indicated properly) accordingly. I'm using Unity 2017.3.1p3. Help? Thanks in advance.
Edit: SOLVED - Apparently, moving the above block of code into LateUpdate() fixed the problem.