Hi All,
I have an object launcher in my project with a prefab attached to it.
During play, i instantiate this object afew times, thus the prefab.
At the end of the party, i destroy all the instantiated objects to start a new party.
But i want to instantiate the object again, i get an error message stating that the prefab is null
any idea what can go wrong?
is there a way to explicitely reattach the prefab to the object during play to overcome this
any help gracefully welcomed!
If you can post the script where you are doing the instantiate/destroy action, that would help. It sounds like you are Destroying the reference to the prefab itself, rather than the instantiated Instance of it. Here’s a very rough cut at what I’m getting at, don’t have much time to write right now:
public class GameController : MonoBehaviour
{
// wire up in inspector
public GameObject prefabReference;
// ref to object that we will create and destroy
private GameObject prefabInstance;
void Start()
{
prefabInstance = Instantiate(prefabReference..etc
}
void PartyOver()
{
Destroy(prefabInstance);
}
void RestartParty()
{
prefabInstance = Instantiate(prefabReference..
}
}
to instantiate i do like this:
var bouleComputer:Rigidbody;
function createBoule() {
n++;
bouleComputer=(Instantiate(bouleComputer, new Vector3(0,10,10), transform.rotation));
bouleComputer.name="boule_computer"+n;
bouleComputer.rigidbody.velocity=transform.TransformDirection(Vector3(throwForceX,throwForceY,throwForceZ));
}
to destroy, i do (from an outside script):
function newGame() {
for (var i:int=1;i<=BouleComputerThrowScript.n;i++) {
Destroy(GameObject.Find("boule_computer"+i));
}
BouleComputerThrowScript.n=0;
}
my var is a Rigidbody, yours is a GameObject, could that be the clue?
i try to declare the var as a GameObject but i get a class cast exception
can you explain how to use a GameObject rather than a rigidbody?
i used an empty GameObject to which i associated a script and a prefab that has a rigidbody, i refer to it as a rigidbody.
shall i declare the rigidbody at the GameObject level rather than the prefab?
cheers, benoit
i got it thanks to your code:
i was using the same reference for the prefab and the object instantiated
thanks again for your help