I’m INSTATIATING this lil plane with an animation:
The animation just works by moving the UV along.
When the UV reaches the end:
if (index == totalCells) {
Destroy(gameObject);
}
and I think instead of removing the plane from the game world, it’s removing it from my library of assets (or something?) because it’ll only instatiate it once. What am I doing wrong?
Boy I’m learning how to make games in 3D for the first time and I am screwin up all over the dang place!
Destroy does exactly that, it deletes it from the scene. Make sure that you are referencing whatever it is that you want to destroy. Destroy() will destroy the gameObject and everything included with it.
Ok here’s what I’m trying to do: I wanna spawn this plane with an animation on it, and then delete it when the animation’s done. So it doesn’t just hang there and loop forever. Like you would do with a squib or explosion effect.
In the original post I’m showing that it does delete, but then also never spawns another one. So that’s the problem.
Slash1 = Instantiate(Slash1, transform.position, transform.rotation) as Transform;
This is Instantiating a gameobject (Slash1) and overwriting Slash1 as that gameobject(Clone).
You need to keep a ref to the prefab Slash1 and not overwrite it with your instantiated object.
Transform TempSlash1 = Instantiate(Slash1, transform.position, transform.rotation) as Transform;
Then you can delete it by Destroy(TempSlash1); but only if TempSlash1 is saved somewhere or you use it in the same function.
To delete the Instantiated Object you need to save the reference to it or call Destroy() on a script on that object.
Edit:
Noticed this
Slash1 = Instantiate(Slash1, transform.position, transform.rotation) as Transform;
Slash1.transform.Translate(1,0,0); //transform is not needed as Slash1 is a Transform.
Ok- Both those worked. So that’s great. I’m gonna make sure the name of the newly instantiated object isn’t the same as the prefab, because I understand what I’m fixing there.
It still shows in the Hierarchy panel as “Slash1(clone)”, so that’s confusing- but it works now so OK whatever.
I thought “transform” was just an object property, I didn’t know things could be “a transform”. I thought everything I was using was an object.
A seperate problem is that the animation keeps starting on the wrong frame instead of frame 1…
I think the code for animating the texture is running on the PREFAB, so the one I’m spawning just start on whatever frame the prefab happens to be on… I’m GUESSING that’s what’s happening, but I barely know.
As a thought, you could also just give your instantiated object a name and then tell the destroy command to destroy that specific name. That might solve the problem for you. It would look something like this:
if(Input.GetButtonDown(“Fire1”)){
animation.Play(“Slash1”);
GameObject SlashFlash = Instantiate(Slash1, transform.position, transform.rotation) as GameObject;
SlashFlash.name = newSlash;
newSlash.transform.Translate(1,0,0);
newSlash.transform.Rotate(90,90,0);
Destroy(newSlash);
}
I’m no wiz coder so no warranty on the above code, but it might get you moving in the right direction.
Try starting your animation.Play("Slash1"); after instantiating it. It may be starting it’s animation before it’s created, or it’s animation frames could be wrong. (Just a guess.)