- Home /
destroying an object instantiated that came out of spawn point
hi i have a problem here i want to destroy an object that comes out of an spawn point im trying to do 3 different spawn points and so they can throw different objects from them that will hit the player the spawn is fine and everything but as i said i cant make them dissapear if i use destroy(gameobject) it will destroy the spawner and well that is not good. in any case heres my code
var timeToWait : float = 1; var timeToWait2 : float = 4; private var timer : float = 0; private var timer2 : float = 0; var beginSpawn : boolean = false; var prefab : Transform; private var spawnPoint : Vector3; function Update() { if(beginSpawn) { spawnPoint = this.transform.position; timer += 1*Time.deltaTime; timer2 += 1*Time.deltaTime; if(timer >= timeToWait) { prefab =Instantiate (prefab, spawnPoint, this.transform.rotation); timer =0; }
prefab.transform.position.z -=0.1;
if(timer2 >= timeToWait2)
{
//Destroy(GameObject.FindWithTag("Clone"));
timer2 =0;
}
}
}
to make it easier what i want to do is destroy the things that come out of the spawner after a certain time and thats about all thank you (oh yeah this code is inside my spawners and i want to use Transform for this tyvm)
if you cant understand what im trying to do tell me and i will try to explain >.<
Answer by AngryOldMan · Feb 27, 2011 at 04:31 AM
Okay im not sure exactly what your after but it sounds like you could benefit from a little bit more information. In this is example lets say we have a game with a cannon that fires a cannonball every 5 seconds, the player does not control this object or its actions.
In your scene First create a cylinder gameobject. This will be our cannon.Next create a new javascript. Name it something like "CannonScript". In the project window drag the CannonScript onto the Cannon in the hierarchy window. Next (with the CannonScript selected) go to "edit" in the inspector window this will open your script editor of choice. In the script paste this code. Make sure you read and understand it!
//this is where we will specify the cannonball prefab in the inspector
var CannonballPrefab : Rigidbody;
//this is when the next shot will be instantiated, it goes up everytime the function succesfully plays through
var nextShotTime : float = 4.0;
//this is the time inbetween each instantiate, you can edit this time in the inspector to make the time between shots longer
var timeBetweenShots : float = 4.0;
//this is how fast your cannonball will be fired, change this in the inspector depending on how big your level is
var CannonballSpeed : float = 2;
//change this depending on how big your cylinder is,this is for if it's scaled by 1
var offset : float = 1.6;
//the actions in this function are performed every frame
function Update()
{
//exectued when the variable "nextShotTime" is less than the time passed in the game
if (nextShotTime < Time.time)
{
//this first line adds the "timeBetweenShots" variable to the "nextShotTime" variable
nextShotTime = Time.time + timeBetweenShots;
//this is where you instantiate the CannonballPrefab which you should have set, for the script, in the inspector
//this line states that the CannonballPrefab will be instantiated on the "up/Y" axis of the Cannon, the offset is so that it comes out of the end.
var Cannonball = Instantiate(CannonballPrefab, transform.position + transform.up*offset, transform.rotation);
//this line gives the Cannonball, which we have instatiated, a direction and speed to go.
Cannonball.rigidbody.velocity = transform.up * CannonballSpeed;
//this is a debug log, you can see if the script has worked as a message will pop up in the bottom right corner of the Unity editor
Debug.Log ("Cannon has just fired");
}
}
Next you need a cannonball so create a sphere in the scene,call it Cannonball, and with it highlighted in the hierarchy give it a rigidbody (Component ~> Physics ~> Rigidbody) Now create a new prefab in the project window and drag the Cannonball from the hierarchy window onto the prefab in the project window. With the Cannon selected, drag the newly made Cannonball prefab into the approriate slot under the Cannon's CannonScript component. Then create a new javascript to be attached to the Cannonball. This script is for destroying the Cannonball whenever it collides with something, for example your player or the ground.
function OnCollisionEnter(collision : Collision) { Destroy(gameObject);
}
Press play and you should now see the Cannon firing the Cannonball(Clone) in whatever direction the Y axis is pointing. If not check the debug log is there. If the debug log is not there check for error messages. If the debug log is there then adjust the Cannon's variables in the inspector while the game is running (during runtime) until you can see it. Once you can see the Cannonballs then you can start playing around with the different elements like shot speed and the mass and gravity on the rigidbody. You can also create 3 prefabs of the Cannon, the same way you made a prefab of the cannonball, and put a different type of cannonball and different shot settings on each cannon prefab.
yeah i think im doing that but what i dont get is if you are telling the object to get destroyed i mean the cannonball then how can you still keep on producing them if they are not anymore on the scene because i need them to get destroyed on time and not on collision
oh wait so u can make the prefabs like that ok ty this solved the question tyvm
put the script "tool55" suggested on your cannonball prefab and that should give you the effect you want
Answer by AngryOldMan · Feb 27, 2011 at 12:02 AM
put the destory object script on the prefab of what ever it is your instantiating. So something like
function Awake
{
yield WaitForSeconds (4);
Destroy (GameObject);
}
WARNING this is just sample code you will have to assign your own variables and such.
this wont work because i dont know how to make an object from something that doesnt exists and that will delete the parent object and i wont be able to make copies of something that doesnt exists :S but ty
This should work but you don't need yield WaitForSeconds(). The Destroy function has a timer built in. Couldn't be easier. See below.
Answer by tool55 · Feb 27, 2011 at 03:09 AM
Your spawned objects are prefabs. Just attach the script to the prefab, then every instance will have the script attached.
var destructTime: float;
function Awake() { Destroy (gameObject, destructTime) }
yeah i tried that but as i said in the top comment when i destroy the object i can no longer instantiate more of them since the object no longer exists there
Your answer
