Learning C# in Unity, I keep destroying objects wrong

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!

When you do Destroy(gameObject); you’re destroying the object that the script is on. Is that what you want?

More likely, you wanna find the gameobject which represents the plane and destroy that.

So when I instatiate this plane, I should give it a unique name, and then make sure to Destroy(“Name!”); ?

What exactly are you trying to do?

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.

Are you instantiating another? Show your code.

Hmm, can you show us the piece of code? It depends on how you’re instantiating it, I think

if(Input.GetButtonDown("Fire1"))
{		
	animation.Play("Slash1"); 

	Slash1 = Instantiate(Slash1, transform.position, transform.rotation) as Transform;
	Slash1.transform.Translate(1,0,0);
	Slash1.transform.Rotate(90,90,0);
}

Took me ages to figure out how to instantiate, I think I accidentally followed a javascript tutorial like 3 times!

Try instantiating a gameObject, because that’s probably what your destroying.

Something like this:

GameObject itemObj = GameObject.Instantiate(objectPrefab) as GameObject;

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.

Anyway I’m gonna take a break from this.

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.

Nah I tried that before, there’s no way of checking which frame the animation’s on FROM the guy prefab.

So I couldn’t destroy it on the last frame.

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.)

No I mean the 2D plane, not the 3D guy.

The 2D plane is animated through the complex UV-manipulation code I put on pastebin and linked to before.

When you instantiate stuff it creates it as “Name”(clone) in the hierarchy.

transform is a shortcut in GameObject that returns the component Transform on the gameobject.

Instantiating any Component automatically generates a GameObject as that is the base that the components use.

Anything that inherits from Monobehavior is a Component.

The UV’s may be getting changed on the shared material level. Think its something like render.mesh vs render.sharedmesh