- Home /
How to get info from child triggers?
I have a parent and it has a script and three childs with colliders that are triggers. How can I get info about the child triggers colliding with other objects by name?
For example OnTriggerEnter(child's name). That's not how you would write it, but I hope it's more clear what I want to do.
Answer by Owen-Reynolds · Mar 28, 2012 at 10:05 PM
There might be a good way, but I've just been writing a script for the child and having it tell the parent. Ex:
// In parent:
// NOTE: child triggers will call us here:
void public handleTrigger(int whichTrigger, Collider cc) { ... }
// Generic child trigger script:
public int trigNum = 0; // set to 0, 1, 2 ... for each child trigger
void OnTriggerEnter(Collider cc) {
// pass it along to the parent:
transform.parent.GetComponent<parentScriptName>().handleTrigger(trigNum, cc);
}
Actually, I wrote two nearly identical scripts (they call different parent functions.) If I was thinking, I'd have combined them into one, like above.
I understand what that would do, but can't seem to get it to work. I'm not familiar with "cc" and does "..." mean in that you write your code in that? I'm trying to learn Javascript.
I got it to acknowledge the child colliders by using this:
private var canYouWalkRight = true; var rightWall : GameObject; rightWall = GameObject.Find("rightWallCollider"); function OnTriggerEnter(Collider) { if (rightWall.collider) canYouWalkRight = false;}
But how can I tell it canYouWalkRight is true?
Ah...my suggestion is C#. cc
is just the name I picked for the variable. Not important.
The idea is, if you want to know which thing hit (and I think you have to do it like this for triggers) you need a separate script for the kids, which detects the hit and "talks to" the parent. You can look up how one script talks to another (in JS it's a little different, but easy to find.)
Your answer
Follow this Question
Related Questions
Controller Collider Script 1 Answer
Extracting child trigger from compound collision 0 Answers
Detect if out of trigger 2 Answers
Action activates trigger. 1 Answer