Hi.
I came across this issue, when I tried to make a simple script that instantiates a different object and destroys the current object when the player is close enough. The instantiate function keeps instantiating new copies of the same object (when called in the Update() function), even after the object is destroyed. Would calling instantiate in a different function react differently?
EDIT: Calling instantiate in a different function, rather than Update() has the same result. Here is my script (JavaScript):
public enum typeOfDectruction {destroyOnly, destroySound, destroyReplace, destroyReplaceSound}
var destructionType : typeOfDectruction;
var replacementPrefab : Transform;
var soundObjectPrefab : Transform;
var distanceToDestroy : float = 5;
var objectToDestroy : Transform;
var playerCamera : Transform;
function Update () {
var dist = Vector3.Distance(objectToDestroy.position, playerCamera.position);
if(destructionType == typeOfDectruction.destroyReplaceSound) {
if(dist < distanceToDestroy) {
DestroyReplaceSound();
}
}
if(destructionType == typeOfDectruction.destroyReplace) {
if(dist < distanceToDestroy) {
DestroyReplace();
}
}
if(destructionType == typeOfDectruction.destroySound) {
if(dist < distanceToDestroy) {
DestroySound();
}
}
if(destructionType == typeOfDectruction.destroyOnly) {
if(dist < distanceToDestroy) {
DestroyOnly();
}
}
}
function DestroyOnly () {
Destroy(objectToDestroy);
}
function DestroySound () {
Instantiate(soundObjectPrefab, transform.position, transform.rotation);
Destroy(objectToDestroy);
}
function DestroyReplace () {
Instantiate(replacementPrefab, transform.position, transform.rotation);
Destroy(objectToDestroy);
}
function DestroyReplaceSound () {
Instantiate(replacementPrefab, transform.position, transform.rotation);
Instantiate(soundObjectPrefab, transform.position, transform.rotation);
Destroy(objectToDestroy);
}
asked
May 10 '12 at 06:04 PM
LukaKotar
731
●
29
●
48
●
54
Would help to know how exactly you are doing it right now.
Are you quite sure of that? This is probably a good time to paste in some code we can troubleshoot. If you can't figure out the formatting, here, there is always Pastebin.
Okay, I'll edit the question.