I have a model made up of numerous cubes. I can see each of these individual cubes inside of the model within the hierarchy pane.
I am looking for a way to access these individual cubes with javascript so that I can separate them from the model and apply physics to them.
I have no clue where to even start with this task so any help at all would be greatly appreciated.
Thanks.
If I understand your question correctly you would start your script with something like the following: First add a Rigidbody (and Collider if necessary) to each cube. Then start your script like this:
var cube1 : Rigidbody;
var cube2 : Rigidbody;
var cube3 : Rigidbody;
... and so forth
When you’re done with the script, just grab a cube from the hierarchy window and drag it to a “slot” in your script inspector and repeat for all your cubes.
Did I answer your question or did I miss something?
I might not have been clear enough. I am looking for a way to basically loop through all of the cubes in the model. I would like to not have to create a variable for each cube since I do not know how many cubes each model will have yet. Some models will have over 50 cubes also. They cannot have any physics applied to them initially because they will be moving as part of the model.
If you create the cubes procedurally with Instantiate then you can store all the resulting cubes in an array, and loop through that.
For an existing hierarchy, putting something like this on the root object could do the trick:
// Quick and dirty code
var Cubes;
function Awake()
{
Cubes = Array();
GatherChildren(transform);
}
function GatherChildren( trans )
{
for( child in trans )
{
Cubes.Add(child);
GatherChildren(child);
}
}
This script basically recursively traverses all the transforms you have in the hierarchy and collects them in an array called ‘Cubes’.
I tried out this code (and modified it) and now I have a problem:
// Quick and dirty code
var Cubes;
var hitPoints = 100;
private var points : float;
function Awake()
{
Cubes = Array();
GatherChildren(transform);
}
function GatherChildren( trans )
{
for( child in trans )
{
Cubes.Add(child);
GatherChildren(child);
Cubes.Broadcastmessage("KinematicOn");
rigidbody.isKinematic = true;
}
}
function ApplyDamage (damage : float) {
if (points <= 0.0)
return;
points = hitPoints - damage;
if (points < hitPoints) {
rigidbody.isKinematic = false;
Cubes.Broadcastmessage("KinematicOff");
}
}
That’s the code for the parent gameObject, this one is for the children:
function KinematicOn () {
rigidbody.isKinematic = true;
}
function KinematicOff () {
rigidbody.isKinematic = false;
}
@script RequireComponent(Rigidbody)
The code should turn on the isKinematic variable of the rigidbody, and when hit, it should be turned off, but something isn’t working and there’s no error message… :?
I see, I should have commented my code better. Looking at what you did, I reworked my example, which works in a simple setup that I made here.
// Another quick and dirty code
var Cubes;
var hitPoints = 100;
private var points : float;
function Awake()
{
// Create and initialize an array, that will
// hold all the transforms that make up this hierarchy
Cubes = Array();
// Add ourselves and all the child objects to the array
Cubes.Add(transform);
GatherChildren(transform);
// Set all objects to be Kinematic by default
SetKinematicForAll(true);
// Simulate death of this entity within 3 seconds as a test
Invoke("OnDeath", 3.0);
}
// Assume this function would be called on death of the entity
function OnDeath()
{
// Enable physics for all objects in the hierarchy
SetKinematicForAll(false);
// Apply some random force, so objects start moving
for (cube in Cubes)
{
cube.rigidbody.AddForce(Random.insideUnitSphere*1000);
}
}
// Recursive function to fill the Cubes array
function GatherChildren( trans )
{
for( child in trans )
{
// Make sure all children have a Rigidbody component
if (!child.rigidbody)
child.gameObject.AddComponent(Rigidbody);
Cubes.Add(child);
GatherChildren(child);
}
}
// Traverses the Cubes array and sets the state of isKinematic as requested
function SetKinematicForAll( state : boolean )
{
for (cube in Cubes)
{
cube.rigidbody.isKinematic = state;
}
}
This script should be added to the root of the hierarchy again and should have everything all in one, so you don’t need to pass messages between objects to make it work.
Thanks for help, Indy138
That’s working great!