Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Spawn objects only once

Discussion in 'Scripting' started by Booja_private_cit, Aug 10, 2022.

  1. Booja_private_cit

    Booja_private_cit

    Joined:
    Jun 7, 2022
    Posts:
    8
    Greetings, i am a noob, please assist , I had the lesson on junior programming, and trying to make a math game. It can only spawn the answers of the 2X table once ( 2,4,6.....till 24) I got it to spawn where it is suppose to spawn with the move speed, but gets duplicates and it doesn't stop after all 12 answers were spawned.
    I am using Unity 2022.1.12f1.
    After many forums I changed some code from [InvokeRepeating("SpawnRandommath", startDelay, spawnInterval);] to [Invoke(nameof(SpawnRandommath), startDelay);], they suggested it is a better coding format.

    But I still need help with each number ( the answer) may only spawn once. I have them as Prefabs ( just like I saw in the tutorial).
    Code (csharp):
    1.  
    2. using System.Collections;
    3.  
    4. using System.Collections.Generic;
    5.  
    6. using UnityEngine;
    7.  
    8.  
    9. public class SpawnManager : MonoBehaviour
    10.  
    11. {
    12.  
    13.     public GameObject[] mathPrefabs;
    14.  
    15.     private float spawnRangeX = 23;
    16.  
    17.     private float spawnPosZ = 130;
    18.  
    19.     private float startDelay = 1;
    20.  
    21.     private float spawnInterval = 1.5f;
    22.  
    23.    
    24.  
    25.  
    26.  
    27.     // Start is called before the first frame update
    28.  
    29.     void Start()
    30.  
    31.     {
    32.  
    33.  
    34.         Invoke(nameof(SpawnRandommath), startDelay);
    35.  
    36.  
    37.  
    38.     }
    39.  
    40.  
    41.     // Update is called once per frame
    42.  
    43.     void Update()
    44.  
    45.     {
    46.  
    47.     }
    48.  
    49.  
    50.  
    51.  
    52.     void SpawnRandommath()
    53.  
    54.     {
    55.  
    56.         int mathIndex = Random.Range(0, mathPrefabs.Length);
    57.  
    58.         Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX),
    59.  
    60.                 4, spawnPosZ);
    61.  
    62.  
    63.         Instantiate(mathPrefabs[mathIndex], spawnPos,
    64.  
    65.                 mathPrefabs[mathIndex].transform.rotation);
    66.  
    67.  
    68.         Invoke(nameof(SpawnRandommath), spawnInterval);
    69.  
    70.     }
    71.  
    72. }
    73.  
    74.  

    Thank you
     

    Attached Files:

    Last edited: Aug 10, 2022
  2. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
  3. Booja_private_cit

    Booja_private_cit

    Joined:
    Jun 7, 2022
    Posts:
    8
    Thank you
     
  4. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,596
    You need to keep track of which prefabs you've already created. One way to do this is to use a HashSet to efficiently check whether a value has been used.
     
  5. Booja_private_cit

    Booja_private_cit

    Joined:
    Jun 7, 2022
    Posts:
    8
    Thank you will give it a try
     
  6. FootSteps

    FootSteps

    Joined:
    Aug 19, 2014
    Posts:
    12
    Here is an example of what you could do. As you know from the previous person you need to keep track of what prefabs you have already spawned.

    Another way, is you can change your array to a list. So you can resize it and remove things you have spawned already. So as long as there are prefabs in your list, you can keep looping and spawning. Until you have 0 elements left. So all prefabs in your list haven't been spawned each time you call your spawn method.

    You need to restart play, or refill the list if you want to keep using it though.

    Here is an example.


    Code (CSharp):
    1.     public List<GameObject> mathPrefabs;
    2.     void SpawnRandommath()
    3.     {
    4.         int mathIndex = Random.Range(0, mathPrefabs.Count);
    5.  
    6.         Vector3 spawnPos = new Vector3(Random.Range(-spawnRangeX, spawnRangeX), 4, spawnPosZ);
    7.  
    8.         Instantiate(mathPrefabs[mathIndex], spawnPos, mathPrefabs[mathIndex].transform.rotation);
    9.  
    10.         //Remove the element from the list, so it doesn't show up again
    11.         mathPrefabs.RemoveAt(mathIndex);
    12.  
    13.         //You want to check if there are still items in your list. So you know when to stop
    14.         if (mathPrefabs.Count > 0)
    15.         {
    16.             Invoke(nameof(SpawnRandommath), spawnInterval);
    17.         }
    18.         else
    19.             Debug.Log("DONE Spawning random math");
    20.     }
     
  7. Booja_private_cit

    Booja_private_cit

    Joined:
    Jun 7, 2022
    Posts:
    8
  8. Booja_private_cit

    Booja_private_cit

    Joined:
    Jun 7, 2022
    Posts:
    8

    Thank you
     
  9. Booja_private_cit

    Booja_private_cit

    Joined:
    Jun 7, 2022
    Posts:
    8

    I applied your suggestion, Thank you it worked great.