resetting Prefab gravity

Hi,

I have a prefab ball when it falls and hit the death area , I want to remove it. Then create a new one.

The issue I can remove it Fine. I create a new one fine. But it does not move..I believe it has to do with the gravity, it is not resetting back to true.

Here is the code

// when hit the death area remove it

function OnCollisionEnter(mycollision: Collision) {

if(mycollision.gameObject.tag == "death"){
rigidbody.useGravity=false;
Destroy(gameObject);

// call for a new ball
reappeaer();
}

}

// please I need a new ball

var prefab : GameObject;
function reappeaer(){

// set the ball in this location with this rotation
var position = Vector3(0, 2, 0);
var ball = Instantiate(prefab, position, Quaternion.identity);

// wait please give the player a chance
// the ball should not move before 3 seconds by turning off its gravity
yield WaitForSeconds(3);

// turn gravity back on ..let's play
ball.rigidbody.useGravity = true;

}

Thank you

That can't be the reason. A prefab is just a blueprint for an instance. All changes to an instance of that prefab have no effect on the prefab itself. If you have your original object still in the scene you may have changed something in the editor but you haven't applied the changes to the prefab.

In general: when creating or designing an object in the scene and then drag-drop it into a prefab, the object itself gets an instance of that prefab. It's no longer the "master". If you change the instance in the scene you have to press the apply button at the top of the inspector to apply the changes to the prefab.

Or you select the prefab in you asset folder and change the settings directly at the prefab but it you for example changed the position of an instance in the scene it will not get updated. Every change you do to an instance that differs from the prefab should be highlighted bold.

Hope that gives you a little overview ;)

EDIT 2011.01.27@23:40

I finally discovered your problem:

When starting a coroutine (in JS that would happen automatically) it is bound to the MonoBehavior-script that called the coroutine. The Problem in your case is that you start the coroutine but you've used Destroy on that gameobject. The Destroy actually destroys your Gameobject right before the next frame starts. When the GameObject gets destroyed the coroutine gets deleted too.

You can use Start() to execute your coroutine in the new created ball (which makes even more sense).

// File is named: "Test"
var waitTime : float = 0;
var prefab : GameObject;

function Start()
{
    rigidbody.useGravity = false;

    yield WaitForSeconds(waitTime);

    rigidbody.useGravity = true;
}

function reappeaer()
{
    // set the ball in this location with this rotation
    var position = Vector3(0, 2, 0);
    var ball = Instantiate(prefab, position, Quaternion.identity);
    // Set the waitTime to 3 seconds, the new ball will wait that time
    ball.GetComponent(Test).waitTime = 3;
}

function OnCollisionEnter(mycollision: Collision)
{
    if(mycollision.gameObject.tag == "death")
    {
        rigidbody.useGravity=false;
        // call for a new ball
        Destroy(gameObject);

        reappeaer();
    }
}

But there's another big issue: the reference to the prefab. During cloning an object (from a prefab or another object) all references that have been set in the editor that refers to the object itself (like our prefab variable: it holds the reference to our own prefab) gets adjusted to match the new cloned object. (hell what a sentence)

In other words: after Instantiate() the prefab variable of your new object "ball" points not longer to the prefab, but to ourself. That means when this ball gets cloned again it will be cloned from this object and not from our prefab.

To fight this behaviour you can build a singleton prefab manager which holds a reference to our prefab and we can use this variable to clone our prefab.

// The simplest singleton without checking
// scriptfile is named: "PrefabManager"

static var manager : PrefabManager = null;
var ballPrefab : GameObject;

function Awake() {
    manager = this;
}

Now we can use this manager to get our prefab ref.

function reappeaer()
{
    var position = Vector3(0, 2, 0);
    var ball = Instantiate(PrefabManager.manager.ballPrefab, position, Quaternion.identity);
    ball.GetComponent(Test).waitTime = 3;
}

Note: you have to create an empty GameObject, put the PrefabManager script on it and assign your ballprefab to this manager.

Damn, self reproduction is almost as difficult as in real life :D

good luck