I have two questions…
First, I’m changing several sprites like this;
myRenderer.sprite = sprites [Random.Range (0, 19)];
I was planning to Destroy and Instantiate a new one, but this is working with such little overhead I’d like keep. Am I going to cause a Stack problem or is previous sprite deleted? These objects travel across the screen, once off screen they are move back to the other side, the sprite is changed and a new random speed is applied.
Other items I am creating a new instance of a prefab. What is the proper way to keep a reference to the instance for animation and to destroy? I’ll need public reference to access in scripts.
Thanks,
Don’t destroy and create objects if you can avoid it. Simply assigning a different sprite (as you’re doing here) is much better.
Your question “Am I going to cause a Stack problem or is previous sprite deleted?” is based on a false premise. There is no previous sprite here; you’re merely making a reference to an existing sprite in an array (and all the sprites in that array continue to exist, of course).
In general, you shouldn’t need to. The object should animate itself, based on scripts that are already included with the prefab. If that doesn’t seem like it works for you, I think you’ll need to post more details.
Ok, on a collision, I’m instantiating a prefab object that is in one script. I want to Destroy it within the second script. I need a reference to the instance that was created in the first. That being said I guess I can use GameObject.Active(true/false) instead.
Still not enough. Why do you want to Destroy it in a second script? What are the responsibilities of these two scripts, and on what objects do they live?
I have two different prefab objects that can kill the player, I’m adding an additional sprite with an animation on it to dramatize the dying process in addition to the two colliding objects. I have the animation play once and the player sprite is removed and the last frame of the third sprite replaces the original player object. I’m using active(true/false) and everything is peachie.
You might want to add the use of instantiate and destroy to your pet peeves in many of the examples. Kind of wondered why you would destroy an object to make it again. I assumed that was the thing to do. Have to take a step back and redo my redos. I don’t need any more practice deleting code, I’ve mastered that skill.
I’m only a week and half into working on a game and I’ve found that I’ve been ‘doing it wrong’ and forced to reimplement the incorrect portions twice now. The really painful part of the learning curve is the repeated tossing out of hours of work and redoing, just so you can proceed with the areas you were working on previously.
Well, that’s just life. Apparently my sympathy is all used up today. 
But let’s dive in more on this “need to keep a reference” business. I’m still not seeing it. If an object kills the player by colliding with it, it gets a reference at that time, in the collision event. Or if you’re doing your own collision handling, then you already have (somehow) a set of objects you’re checking for collisions — again, you have a reference there.
Once you have the thing that’s colliding, you can just call GetComponent() or whatever on it, to get the player script, which you then inform of the unfortunate event. That script (which lives on the player object) can then commit the actual hari-kari.
The object I needed the reference for was a third sprite that had an animation on it. I was trying to instantiate it on a collision and Destroy it when resetting the game. I have it working now and I’m not destroying the object. Once I found out it isn’t necessarily proper etiquette to Destroy objects just to remove from view, I worked things out. Almost got things back so I can pick up where I left off.
OK, glad you’re not stuck. Here are just a few tidbits you may or may not know.
- No need to destroy objects when changing/reloading the scene, or quitting the game; objects will be destroyed automatically in these cases.
- It sometimes happens that you want to destroy an object a certain amount of time after it’s created (e.g., because it’s an explosion effect or some such). You can call Destroy right away, but pass in a delay, condemning it to be automatically destroyed that many seconds later.
- In other cases, of course, you can just attach a script that destroys the object when certain conditions are met (end of the animation is reached, it passes out of view, etc.). I tend to have a bunch of these scripts lying around, and just pick the right one for the job.