I'm trying to make a little game where a complex car shoots another complex car. There are many joints in this car(s). I want them to all disable upon a condition (when the force of the bullet hit is greater than xx) so the car will just fall apart. My problem is the hierarchy of the entire car is very complicated, so I can't just index every single joint.
It's hard to explain, but I want to do something like this (logic steps):
When the bullet hits any part of the car, a function is called from that child. This function tells the car (the absolute parent for the whole car) it was hit, as a whole, and then a function in the parents script disables all the joints inside that car. But how can I call a function from a child of an unknown generation (I don't know how much of a child it is, could be 1, 2, 3, 4th child model)?
asked Feb 11 '10 at 11:05 PM
If your car has one parent and this parent is a "root" in your hierarchy (it has no parent itself) you can do something like this:
If the car parent is not == null just look for the parents name instead null.
answered Feb 11 '10 at 11:52 PM
SendMessageUpwards() is what you need for this. have the child do something like this
Only the car object should have an ApplyDamage method/function so other children will ignore it and just pass the message up. Since this message is not passe that often it is OK to send messages around. If you will be passing many messages each frame you should cache a pointer to car in the childern and call the applyDamage directly.
answered Feb 12 '10 at 01:11 AM
spree's answer is correct, but a simpler way would be to just use the root variable of Transform, which returns the topmost transform, i.e the parent of your hierarchy
from there you can access its components etc. etc.
answered Feb 12 '10 at 03:40 AM
define the function that you want in a script and attach that script to your parent GameObject. the name of that function should be unique. you can call GameObject.SendMessageUpwards in any of childern to call that function in their parents. if they have multiple parents then they'll try to call that in all of them so you should attach the script just to the parent that you want to have the script. also you can get the transform.root to get the root of your gameObject and then use GetComponent to get a reference to the script that you want and call its functions. this method is better and faster. call transform.root and GetComponent once in start function of all children and store the result in a variable and then in all collision use that value. avoid using SendMessage functions as much as possible. the function is too slow. when you call it, it will search all your child/parent gameObjects and all of their scripts for the function that you specified.
answered Feb 26 '10 at 05:05 AM