- Home /
TURN GRAVITY ON!
Lads,
I'm using projectiles to destroy chunks out of a wall. Once 100 chunks have been destroyed I'd like to turn gravity 'on' so the wall collapses. Having a hard time getting 'er done. Here's my script. Any thoughts for a scripting noob?
function OnCollisionEnter(collision : Collision)
{
if (collision.gameObject.tag == "missile_1")
{
Destroy (gameObject);
//Instantiate (smokePrefab, transform.position, transform.rotation);
//Instantiate (Sphere, transform.position, transform.rotation);
objectDestroyed++ ;
print (objectDestroyed);
if( objectDestroyed >= 100)
{
gameObject.rigidBody.useGravity = true;
}
}
Thanks,
Stef
Answer by Razion · Nov 24, 2012 at 06:45 AM
I'm going to assume you have this script on each of your "wall blocks" that you want destroyed. If that's the case, the real problem is that you have a different objectDestroyed variable in each script. There's no cumulative counter.
You need to have a script that keeps track of that sort of thing on an object that isn't going to be destroyed. (invisible gameobject would be fine). Each time a block gets destroyed, have it modify the objectDestroyed variable in that "master" script. Then check against that variable in your Update function to determine when to flip on gravity.
Your answer
Follow this Question
Related Questions
How do you modify gravity force in this script ? 1 Answer
Disable script on other object 2 Answers
Having trouble turning off gravity through script 1 Answer
Disable a script on collision 2 Answers
(C#)How To Disable Gravity From Script? 4 Answers