Search Unity

how do I spawn in sprites infinitely everywhere and regenerate over time.

Discussion in '2D' started by Dra60n, Oct 12, 2020.

  1. Dra60n

    Dra60n

    Joined:
    Oct 12, 2020
    Posts:
    3
    Basically my game is a mobile game where you move across the screen and you need to collect balls in the air but I need to make the balls to be infinitely spawned everywhere for this to work.... could any one help me with this???
     
  2. Cornysam

    Cornysam

    Joined:
    Feb 8, 2018
    Posts:
    1,465
    Look up Instantiation. Its a basic function that "creates" GameObjects of your choice in locations of your choice. You can create an array of transformation points that are selected randomly. For doing it infinitely, put the instantiation function in a loop so it occurs every X seconds. You don't want to just put it in Update so it happens every frame, that will bog down performance a lot.
     
    Dra60n likes this.
  3. adehm

    adehm

    Joined:
    May 3, 2017
    Posts:
    369
    You would have to instantiate them all before the game begins and then recycle used ones so that it appears infinite.
     
    Dra60n likes this.
  4. Dra60n

    Dra60n

    Joined:
    Oct 12, 2020
    Posts:
    3
    thank you so much
     
  5. Dra60n

    Dra60n

    Joined:
    Oct 12, 2020
    Posts:
    3
    got it thank you
     
  6. Proxima_Centauri

    Proxima_Centauri

    Joined:
    Oct 20, 2020
    Posts:
    42
    You can make a separate method to Intantiate it and use a Coroutine to make it wait for X seconds

    Code (CSharp):
    1. public IEnumerator SpawnObj(float time)
    2.     {
    3.         //Instantiate your object
    4.         yield return new WaitForSeconds(time);
    5.     }
    and call this method with

    Code (CSharp):
    1. StartCoroutine(SpawnObj(time));