- Home /
Cant remove bullets after a set amount of time
I am trying to remove an bullet after a set amount of time but all i get is the error saying it cant delete it due to data loss.
i am using the code
if (timer >= 4)
{
Destroy(Fireball.gameObject);
}
is there a way to remove the bullet that was fired after the set amount of time.
I'm new to Unity but an old-hat at C# and it would seem to me that maybe you just need to Destroy(Fireball) ?? I don't know for sure, that's why I'm only posing this as a comment.
What exactly is unity telling you? Data loss is not an error Unity throws, can you paste it in? :)
Also, by reading the documentation, it appears you can't Destroy a prefab - that's not a prefab is it? (IE, it should be an instance - maybe created FRO$$anonymous$$ a prefab but not the prefab itself) (Using the Instantiate method)
@Dracorat correct, we need more info on what Fireball
is and also are you sure that your timer works? Have you one a Debug.Log
in the statement?
Fireball is a prefab and i can get it to fire. i know the timer worked because i only get the error after the set amount of time.
Answer by Dracorat · Apr 04, 2013 at 04:14 PM
Based upon your confirmation that Fireball is a prefab, I think the problem is that you're attempting to use the prefab directly.
In general, your code should be something like this:
// Inside the class - local variable, public access
public GameObject Fireball; // In the editor window, drop the Fireball prefab to this
// In a method ...
var fireballInstance = Instantiate(this.Fireball);
//Later, when you're done with it and want it gone
Destroy(fireballInstance);
You can't destroy the prefab - and you shouldn't be trying to use a prefab directly. If you placed it in your scene - so that it's an instance, be sure to drag the Instance (from the scene graph) to the script reference, not the Prefab (from the project explorer) to the script reference.
Answer by DryTear · Apr 04, 2013 at 02:04 AM
AutoDestroy.js :
var ExpireTime : float;
function Start(){
yield WaitForSeconds(ExpireTime);
{
Destroy(gameObject);
}
}
Why not:
var ExpireTime : float;
function Start() {
Destroy(gameObject, ExpireTime);
}
Is there any significant performance difference between the two?
Your answer
Follow this Question
Related Questions
shoot bullet at any direction(random) 0 Answers
Destroy bullet on random collision 2 Answers
C# Destroy tag on collision 1 Answer
Remove trees dynamically - stuck with collider 3 Answers
ON hit remove 1 Answer