Hi people, i want to make a destructible object for my game. I want it to break when i hit it but as you know i cannot make it rigid before the impact since the gravity will tear it down as all objets cant stay on their feet without atomic bonding.
So i made a glass shaped sturcture to test and learn unity, i made 2 scripts that work perfectly fine. But i have to write the same code 76 times which is no big deal really, but i wanted to learn the right way to do it so i am here.
The codes i use are:
function React () {
gameObject.AddComponent(Rigidbody);
}
This is the react code that is in every box, 76 of em.
function OnCollisionEnter(col : Collision) {
if(col.gameObject.name == “Collider”)
{
gameObject.Find("1").SendMessage("React");
gameObject.Find("2").SendMessage("React");
gameObject.Find("3").SendMessage("React");
}
}
This goes till 76.What this does is; The object i am hitting the glass is named collider. When the collider hits any of the boxes, all of them suddenly become rigid bodies and it breaks down nicely just as i wanted it to. This code is also in every box, 76 of them. The boxes are named , 1,2,3,4,5… 76 the last one, all in numbers. Now i want to make a loop like this;
function OnCollisionEnter(col : Collision) {
if(col.gameObject.name == "Collider")
{
for(i=1; i<=76; i++){
gameObject.Find(“i”).SendMessage(“React”);
}
}
}
But the problem here is that, the “i” that the gameObject.Find is looking for isnt the “i” in my loop. So i need your help. By using loop and class together or just one of them, can i make the code work for every box with a loop.
I also need some help in adding an “Energy” or “Velocity” barrier to the function. For example a glass wouldnt break if it fell from 5 cm, but it will break if it fells from 1 meter. I need to add some sort of a restriction to the Send.Messege operation. If you can help me with that also. I want the cube glass not to break if isnt hit by “X” joules of energy or “Y m/s” velocity.
Any help is appriciated guys, love you