Home > Software design >  How to make Objects fall out of the sky randomly and then delete themselves when they go off-screen?
How to make Objects fall out of the sky randomly and then delete themselves when they go off-screen?

Time:01-25

For Unity (Lastest Version & C#)

  1. How do I make an object fall from the sky at random places (random x but fixed y) in a 2D game
  2. How do I delete them when they get off-screen and create a timing between objects falling

Whenever I run the program it randomly creates object but keeps naming them this: myObj, myObj(Clone), myObj(Clone)(Clone), myObj(Clone)(Clone)(Clone) every time a new object is instantiated. More importantly the instantiate part keeps running, spawning a bunch of objects and not deleting them.

Also, I just don't get how to delete the objects as soon as they get out of from the camera. Do I have to create a different script for it or smthn?

public class RandomObjectsFalling : MonoBehaviour
{
    public GameObject obj;
    public float timebeforedie = 1f;

    void Start()
    {
        StartCoroutine(SpawnBlocks());
    }


    IEnumerator SpawnBlocks()
    {
        while(x <= 1)
        {
            float randomPosition = Random.Range(-10f, 10f);
            GameObject clone = (GameObject)Instantiate(prefab, new Vector3(randomPosition, 8, 0), Quaternion.identity); //problem is this keeps getting called creating a whole bunch of objects but they don't get destroyed
            yield return new WaitForSeconds(timebeforedie);
            Destroy(clone);
        }
     
    }
}

CodePudding user response:

To make an object fall from the sky at random places in a 2D game, you can use the Instantiate method to create a new instance of the object at a random x-coordinate but fixed y-coordinate. You can use the Random.Range method to generate a random x-coordinate between a certain range.

To delete the objects when they get off-screen, you can use the OnBecameInvisible method, which is called when the object becomes invisible to any camera. You can check if the object is off-screen, and then call the Destroy method to delete it.

Regarding the naming of the objects, this is due to how the Instantiate method works. It creates a new instance of the object and automatically appends "(Clone)" to the name of the object. To avoid this, you can give a unique name to each object when you instantiate it.

Regarding the instantiation part, you are using a while loop with x<=1 which will keep spawning object indefinitely. You need to decide on a number of objects that you want to spawn and use a for loop with a counter instead.

Here is an example of how you can modify your code to achieve this:

public class RandomObjectsFalling : MonoBehaviour
{
    public GameObject obj;
    public float timebeforedie = 1f;
    public int numberOfObjects = 5;

    void Start()
    {
        StartCoroutine(SpawnBlocks());
    }

    IEnumerator SpawnBlocks()
    {
        for (int i = 0; i < numberOfObjects; i  )
        {
            float randomPosition = Random.Range(-10f, 10f);
            GameObject clone = (GameObject)Instantiate(obj, new Vector3(randomPosition, 8, 0), Quaternion.identity);
            clone.name = "FallingObject"   i;
            yield return new WaitForSeconds(timebeforedie);
        }
    }
}

Then you can use the OnBecameInvisible() method on the falling objects which will call the Destroy() method as soon as they get out of the camera.

 void OnBecameInvisible()
{
    Destroy(gameObject);
}

And then you can add the above code to the falling object script.

CodePudding user response:

Use this code to Spawn Your Obj

public GameObject prefab;
public bool startSpawning;
public float spawnDelay;
private float _currentSpawnDelay;

private void Start()
{
    _currentSpawnDelay = spawnDelay;
}

private void Update()
{
    if (startSpawning)
    {
        _currentSpawnDelay -= Time.deltaTime;
        if (_currentSpawnDelay < 0)
        {
            _currentSpawnDelay = spawnDelay;
            var randomPosition = Random.Range(-10f, 10f);
            Instantiate(prefab, new Vector3(randomPosition, 8, 0), Quaternion.identity);
        }
    }
}

And This To Destroy Them But This Code Need To be Attach On Obj You That you want to Spawn

    private void Update()
{
    if (gameObject.transform.position.x < -20)
    {
        Destroy(gameObject);
    }
}

Attach The Destroy Code to your Obj Prefab

  • Related