Know that code is played in a consecutive order, so on the second script you cannot spawn a prefab AFTER destroying the gameobject that holds the script itself.
What you need to do is spawn the prefab before destroying the gameobject, this although seems noticable - it's not, in frames they will happen at exactly same time.
I understand that the second script you show is attached to the gameobject that you collect in game, and after it's destroyed - you want to spawn a prefab. The code would look like this then:
var myCoolPrefab : Transform; //insert your prefab object in here at the inspector
function OnCollisionEnter(col: Collision){
if (col.gameObject.tag == "PlayerMarble"){
Instantiate(myCoolPrefab, transform.position, transform.rotation);
Destroy(this.gameObject);
}
}
Check out the [Instantiate][1] function and spawning prefabs video [here][2].
[1]: http://docs.unity3d.com/Documentation/ScriptReference/Object.Instantiate.html
[2]: https://unity3d.com/learn/tutorials/modules/beginner/scripting/instantiate
↧