- Home /
OnControllerColliderHit function not called? [C#]
Hi! I've got a problem with the "OnControllerColliderHit". I use this function in the FirstPersonController. When the player hit a campfire, campfire must light. It's not working - the function OnControllerColliderHit is not called. Can you tell me what I'm doing wrong? Part of the script:
void OnControlerColliderHit(ControllerColliderHit col)
{
if(col.gameObject.name == "campfire")
{
if(haveMatches)
{
LightFire(col.gameObject);
}
else
{
textHints.SendMessage("ShowHint", "MESSAGE");
}
}
}
void LightFire(GameObject campfire)
{
campfire.GetComponentInChildren<ParticleSystem>().Play();
campfire.audio.Play();
Destroy(matchGUI);
haveMatches = false;
}
The if statements probably aren't having their requirements met, you can use Debug.Log to check
void OnControlerColliderHit(ControllerColliderHit col)
{
Debug.Log(col.gameObject.name);
if(col.gameObject.name == "campfire")
{ //and so on
-_- The name is correctly... Have you got any other ideas?
I try your script and I find something... The DebugLog is not showing any info... How to fix it? The FPC has the charactercontroller component...
Answer by Mortoc · Feb 04, 2014 at 04:58 PM
Typo in the function name: OnControlerColliderHit should be OnControllerColliderHit
Oh... Noob problem... Thank you very much (debuglog is showing info about collision, but my particles systems are still not playing. Can you fix this problem too?
UPDATE to comment: can it be caused my campfire is the child of the empty gameObject called "Builidings" and it have a lot of child (rock1, rock2 etc.)?
if you take the line:
campfire.GetComponentInChildren<ParticleSystem>().Play();
and change it to
var campfireParticles = campfire.GetComponentInChildren<ParticleSystem>();
Debug.Log("particles", campfireParticles);
campfireParticles.Play();
does it select the particle system component you're expecting it to in the debug log?
I probably know what's the problem: when I collide with rock which is the child of the campfire only, the LightFire function isn't called. How can I fix it? Is it possible that can I make the one GameObject of the Parent/Child group?