- Home /
Javascript Detecting a prefab's child's collision
I'm making a simple enemy script {one that just walks around randomly & turns around if it hits a wall} that I plan on turning into a prefab later on so I can spawn multiple of them. It's going to have two colliders: one underneath to tell the enemy whether or not to decrease its Y position {not using Unity's physics}, & one in front of & inside of it to tell the enemy that it's hit a wall & to turn around.
The problem I'm having is that, in the past when I've made multiple colliders for an object I would give them a static variable that the parent can access to see when they've collided with something.
static var onGround = 0;
function OnTriggerStay(other : Collider){
onGround = 1;
}
function OnTriggerExit(other : Collider){
onGround = 0;
}
which has worked fine for objects that don't need to be turned into prefabs, but I imagine loading in multiple of the same enemy that all use the same static variable would make all of them think they're colliding when one of them does. After digging through answers to see if I could access the children's script from the parent, I haven't found anyone that seems to have the same problem.
I've also tried things like
parentTransform = GameObject.Find("enemy1").transform.position.y - 0.002;
& variations of code from people's answers involving parentTransform, but it keeps giving me errors like "unknown identifier". Does anyone know how I can have the parent script detect a child's collision, or have a child's script change the parent's Y position? Remember, I'm limited to whatever code you can use in a prefab so I can't rely on static variables.
Any help is appreciated ^-^
Answer by TanselAltinel · Nov 08, 2017 at 07:38 AM
Hi Jamiemon,
First and foremost, Unityscript (what you refer as Javascript) will be discontinued. So I strongly advise you to switch to C#.
Secondly, avoid using static variables with all you got, unless you absolutely know what you are doing.
Now, for your solution. You can use GetComponentsInChildren that will enable you to get all children objects with your component.
Thus, you will be able to reach the script and its variables instead of using a public static variable.
Thanks for letting me know, I had no clue unityscript would be ending so I started the process of learning C# I'll probably repost a C# version of this question with a better understanding of what I want later
Answer by imrankhanswati · Nov 08, 2017 at 07:44 AM
hello @Jamiemon.
To get collision from child you need to attach another script to child and this will check if child is collided with wall if it is then this will call his parent to change direction.
Child Script:
parentScript parentScript; function OnTriggerExit(other : Collider) { if(other.gameObject.tag == "Wall") parentScript.changeDirection(); }
Parent Script:
function ChangeDirection() { //Change direction.... }
Might this help. if you need actual script for this let me know. good luck. :)
By the way i will recommend to us C# not JS because JS will be deprecated in future so i think using JS in unity is not a good idea.