- Home /
move, destroy self, instantiate again, repeat.
Moving Object that goes to it's destination, destroys itself, and instantiates again doing the same thing
So I have an above average (ok slightly above average) of scripting in Unity. I'm just trying to wrap my head around this concept and I just can't seem to do it for some reason. So I know how to write the code so that one moving platform goes to it's waypoint and then destroys itself, or at least this is basically how I would do it explained below.
First I'd attach the script to the object I want to move. Then I would have an empty game object parented beneath it. I would use this object to hold the position coordinates of where ever I want the platform to translate in space. Using some fancy math I would have a variable that sets the time of how long it takes the platform to get to that location, then I would also use that same variable in a destroy function so that it destroys itself when it reaches it's target location.
I just have no idea how to implement this when the platform destroys itself instantiating another one that does the same thing in an infinite loop. I have something similar happening with another game mechanic where barrels roll and keep spawning after the last one is destroyed. Expect for that I don't have to set a waypoint which I found I can't do with prefabs exactly or I just don't know how.
I hope this makes sense I'm kind of blabbering. Below are the scripts I used for the barrels, one goes on the barrel prefab itself and the other spawns the barrel. I tried to use the same logic I used with them but to no avail. Is there anyway to implement the above?
Barrel Script Below, remember I am not asking about these scripts specifically I just tried to use them as reference to use the same logic on my proposed plan above but I can't exactly get it to work.
var barrelDamage : int = 2; //Integer value of the damage barrel does to broc if OnCollisionEnter function intitiates
var autoDesTime : float = 5; //Time to destroy barrel if it does not hit broc
var hitPoints: float = 4.0;
var explodeParticle: GameObject;
//To auto destroy barrel after "x" seconds
function Start () {
Destroy(gameObject, autoDesTime); //Destroy object in "x" seconds after not hitting Broc
}
//Test for collision with Broc only
function OnCollisionEnter (collision : Collision) {
if(collision.gameObject.tag == "Player") { //Broc needs to have the Player tag
Destroy(gameObject); //Destroy game object after colliding with Broc
Instantiate(explodeParticle, transform.position, Quaternion.Euler(-90,0,0)); //Instantiate particle explosion
Destroy(explodeParticle, .25);
collision.collider.SendMessageUpwards("ApplyDamage", barrelDamage, SendMessageOptions.DontRequireReceiver); //Sends damage to Broc HP script
}
}
function ApplyDamage(damage : float){
Debug.Log("Barrel just took " + damage + " damage!");
if(hitPoints <= 0.0)
return;
hitPoints -= damage;
if (hitPoints <= 0.0) {
Destroy(gameObject);
Instantiate(explodeParticle, transform.position, Quaternion.Euler(-90,0,0)); //Instantiate particle explosion
Destroy(explodeParticle, .25);
}
}
Barrel Spawn Point Below
var spawnTime : float; //Time between Spawns
private var timeSinceSpawn : float; //Holds time since last spawn
var barrel : GameObject; //Object to be instantiated
//Spawn one barrel at the start
function Start () {
Instantiate(barrel, transform.position, transform.rotation);
}
//Function that spawns a new barrel after spawnTime is eclisped
function Update () {
timeSinceSpawn += Time.deltaTime;
if(timeSinceSpawn >= spawnTime) { //Checks every fram to see if spawnTime is more than time since last spawn
// spawn another barrel
Instantiate(barrel, transform.position, transform.rotation);
timeSinceSpawn = 0.0; //resets spawn counter
}
}
if you are "blabbering", click edit and remove every single part of the question that is not absolutely relevant!
it is very possible you need to learn about "pools" ..
http://answers.unity3d.com/questions/321762/how-to-assign-variable-to-a-prefabs-child.html
Additionally, you need to learn about the simple Invoke() commend. Go to unityGE$$anonymous$$S.com for many beginner articles.
hoep it helps
Thanks for that GE$$anonymous$$S website! It will definitely be useful in the future I'll also peruse there first before I ask questions as well!
Answer by iwaldrop · Feb 11, 2013 at 05:29 AM
What you probably need is a controller to spawn, destroy, and respawn for you. With a controller handeling the creation and destruction of objects (instead of the object itself, which just does what it does while it's around) then it should know when and where another object needs to be destroyed.
You can use events to control all objects directly, and you can register all of your active objects in a list to address them individually when necessary. Additionally, you don't need to destroy your objects; just move them back. Instantiation and Destruction are some of the costliest routines that Unity does.
Thank you sir your advice about not destroying and instantiating made a lot of sense! This is the script I came up with and it works quite well. Had to turn off the collision right at the end because of how our character script works for 2d platfor$$anonymous$$g or else he would move wherever the object went! But otherwise thanks! Here is what I wrote maybe others can find use of it.
The way I wrote it I had to make a separate script for each direction I wanted the platform to move but it's not that big of a deal I guess haha.
private var originalPosition : Vector3; //Vector 3 that will end up holding original position of the object
var goToPoint : GameObject; //Game object that will point to where the object needs to translate
var speed : int = 5; //How fast the object will translate to it's destination
//Initialize the object's original starting position to hold
function Start () {
originalPosition = transform.position;
}
function Update () {
transform.Translate(Vector3.left * Time.deltaTime * speed); //Translate the object in " " direction
//If the objects transform becomes greater than the goToPoint's transform in the same direction go to changeCollision function
if(transform.position.x <= goToPoint.transform.position.x) {
changeCollision();
}
}
//Turns collider off yielding for .1 seconds, this is to get Broc off of the collider so he doesn't translate back with it
function changeCollision(){
collider.isTrigger = true;
yield WaitForSeconds(.7); //Yield needed so trigger is off long enough that broc falls off of it
transform.position = originalPosition; //Instantly moves platform to it's original position
collider.isTrigger = false; //Turns collider back on
}
Answer by OmarAlhaddad · Feb 11, 2013 at 06:08 AM
you could use one of the Tween libraries (like iTween), most of them have something like (On Complete) callback, on which you can destroy the object after the tween.
Check out: iTween