rigidbody active on collision

hello,
this is my first post here.
I have some problem to make rigidbody iskinematic false on collision enter.
I have multiple streetlamps in my city where i can dive my car. I want to activate rigidbody on children when i collide with it with the car but something wrong…
All meshes have a collider and are inside an emptigameobject that have a rigidbody assigned. All are inside another gameobgject that contains all city objects:

  • City with RigidBodyControllerscript
    • StreetLamp01 with rigidbody
      • Mesh with collider

I wrote this:

function Start () {
var Rigids : Rigidbody;
Rigids = GetComponentsInChildren (Rigidbody);
Rigids.isKinematic = true;
}

function OnCollisionEnter(collision : Collision) {
for (var Rigids : Rigidbody in rigidbody) {
Rigids.isKinematic = false;
}
}

all suggestions are appreciated
Thanks in advance and sorry for my english!!
Michael

First problem- Rigids (should really be rigids, but that's just convention) is a single Rigidbody, not an array [] of rigidbodies (which is what GetComponentsInChildren returns).

Second problem- you can't just change the values of every member in an array with

rigids.isKinematic

since the array itself does not have any member variables of that name! You have to use a for loop.

Third problem- your syntax for the for loop is all wrong. A for loop is constructed like so-

for (var temporaryName : Rigidbody in rigids)
{
    // do things to temporaryName (which is the name of the current object)
    temporaryName.isKinematic = false;
}

The last bit, after the 'in', tells it what array or list to search through in the for loop. So, instead of putting a reference to the rigidbody attached to the local gameObject ('rigidbody'), you should use your stored Rigidbody[] array.

Fourth problem- you are defining the array of all rigidbodies inside of Start. This means, that as soon as you get to the end of Start() they will be forgotten! You need to put them outside the scope of any function- that is to say, at the top of your script!

private var rigids : Rigidbody[];

function Start () {
    rigids = GetComponentsInChildren(Rigidbody);
    for(var body : Rigidbody in rigids)
    {
        body.isKinematic = true;
    }
}

Then, in your OnCollisionEnter, do something similar to undo the 'kinematic' status of the rigidbodies.

Of course, as with all collisions, make sure that you have at least one non-kinematic rigidbody involved, otherwise the collision won't register. (I know, it sounds obvious, but we really do get a lot of questions about that one).

Here's a version in C#, which I can give more of a guarantee about (since it's a language I actually program in)

// this goes below all the using and monoBehaviour stuff- just use
// the automatically generated stuff, and change the name of it
// to whatever you need it to be called.

private Rigidbody[] rigids;

void Start()
{
    rigids = GetComponentsInChildren<Rigidbody>();
    foreach(Rigidbody body in rigids)
    {
         body.isKinematic = true;
    }
}

void OnCollisionEnter(Collision collision)
{
    if(some condition, depends on how your game works, really)
    {
        foreach(Rigidbody body in rigids)
        {
            body.isKinematic = false;
        }
        // at this point I would set some flag which says that the
        // rigidbodies have already been activated- I might even
        // delete this script instance with Destroy(this), depending
        // on whether it has anything else left to do!
    }
}

thanks syclamoth for putting me on the right path but the problem still remains. It give me an error: InvalidCastException: cannot cast from source type to destination type.
If i press play the game start but the rigidbodys remains non kinematic and i have another problem, for multiple streetlamp i mean thousends. The performance leave me alone!! Perhaps is better to use Sleep() at start and Wake() on collision?

private var rigids : Rigidbody[];
        
        function Start () {
            rigids = GetComponentsInChildren(Rigidbody);
            for(var body : Rigidbody in rigids)
            {
                body.Sleep();
            }
        }

I tried this but rigidbodys remains active and told me: InvalidCastException: cannot cast from source type to destination type.