help with some code
using UnityEngine; using System.Collections;
public class Generate : MonoBehaviour {
public GameObject lightnest;
public float initialDelay;
public float finalDelay;
public float rampDuration;
protected float _delay;
protected float _runTime;
protected float _timeSinceSpawn;
// Use this for initialization
void Start () {
_delay = initialDelay;
_runTime = _timeSinceSpawn = 0.0f;
}
// Update is called once per frame
void Update() {
_runTime += Time.deltaTime;
_timeSinceSpawn += Time.deltaTime;
_delay = Mathf.Lerp(initialDelay, finalDelay, _timeSinceSpawn / rampDuration);
if (_timeSinceSpawn > _delay) Spawn();
}
protected void Spawn()
{
_timeSinceSpawn = 0.0f;
Instantiate(lightnest);
}
}
So this is the code which Im using to generate the pipe thingies, but after i generates it, I want to destroy it after its out of screen or any other possible way, maybe after 3 seconds or something and I want this pipe thingies to generate randomly in different sizes you get me ryt just like flappy bird.
Answer by TBruce · Jun 04, 2016 at 12:53 AM
Use the following script
using UnityEngine;
using System.Collections;
public class DestroyPipeThingy : MonoBehaviour
{
// this function is used to dstroy the GameObject after a certain amount of time
public void DestroyPipeThingyAfterDelay(float delay)
{
if (delay > 0)
{
StartCoroutine(DestroyPipeAfterDelay(delay));
}
}
IEnumerator DestroyPipeAfterDelay(float delay)
{
yield return new WaitForSeconds(delay);
Destroy(gameObject);
}
// this function will destroy the object when it goes off screen
function OnBecameInvisible()
{
Destroy(gameObject);
}
}
Usage
protected void Spawn()
{
_timeSinceSpawn = 0.0f;
GameObject spawnedObject = Instantiate(lightnest);
spawnedObject.AddComponent<DestroyPipeThingy>();
// if you want to destroy the object after a certain amount of time uncomment the following
// spawnedObject.DestroyPipeThingyAfterDelay((initialDelay);
}
but what about the size of the object like flappy bird i can do it within this script ryt if so how thanks in advance
Flappy Bird is a 2D side scroller. It is also an endless game and screens are dynamic. The components will only be added to the pipes on the screen and ad most the next screen. It's not like you are going to have a ton of them especially on screen.
And as soon as the pipe thingy's go off screen they are destroyed so no problem with size.
How do you use it? I gave you that in the usage. See again
// this is inside your Generate class
protected void Spawn()
{
_timeSinceSpawn = 0.0f;
GameObject spawnedObject = Instantiate(lightnest);
spawnedObject.AddComponent<DestroyPipeThingy>();
// if you want to destroy the object after a certain amount of time uncomment the following
// spawnedObject.DestroyPipeThingyAfterDelay((initialDelay);
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612082228im_/https://answers.unity.com/themes/thub/images/avi.jpg)