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

My spawning script isn't consistent. Its not working properly..

Discussion in 'Scripting' started by xXAl-HarereXx, Apr 12, 2020.

  1. xXAl-HarereXx

    xXAl-HarereXx

    Joined:
    Aug 21, 2017
    Posts:
    101
    Some stuff are spawning while others are not. Its so weird and its a real hassle to add any new object to it...
    The problem is in either the spawn or spawnprefab methods.. Any help is apperciated :)


    Code (CSharp):
    1. public class Spawner : MonoBehaviour
    2. {
    3.     [Header("Overall Spawner")]
    4.     [SerializeField] GameObject[] prefabs;
    5.     [SerializeField] float spawnDelay = 3f;
    6.     [SerializeField] int count = 0;
    7.     [SerializeField] int itemsPerScreen = 10;
    8.     [SerializeField] float yOffset;
    9.  
    10.     [Header("Booster")]
    11.     [SerializeField] int boosterCount = 0;
    12.     [SerializeField] int boosterLimit = 2;
    13.     [SerializeField] float boosterSpawnChance = 0.2f;
    14.     [SerializeField] float boosterWithCoinsSpawnChance = 0.1f;
    15.  
    16.     [Header("Rainbow Dust")]
    17.     [SerializeField] int rainbowCount = 0;
    18.     [SerializeField] int rainbowLimit = 2;
    19.     [SerializeField] float rainbowDustSpawnChance = 0.4f;
    20.     [SerializeField] float rainbowDustWithCoinsSpawnChance = 0.1f;
    21.  
    22.     [Header("Coin")]
    23.     [SerializeField] float coinSpawnChance = 0.6f;
    24.     [SerializeField] float multipleCoinsSpawnChance = 0.4f;
    25.  
    26.     [Header("TNT")]
    27.     [SerializeField] int tntCount = 0;
    28.     [SerializeField] int tntLimit = 1;
    29.     [SerializeField] float tntSpawnChance = 0.4f;
    30.  
    31.     [Header("Ghost")]
    32.     [SerializeField] int ghostCount = 0;
    33.     [SerializeField] int ghostLimit = 1;
    34.     [SerializeField] float ghostSpawnChance = 0.15f;
    35.    
    36.     [Header("Barrier")]
    37.     [SerializeField] int barrierCount = 0;
    38.     [SerializeField] int barrierLimit = 1;
    39.     [SerializeField] float barrierSpawnChance = 0.5f;
    40.  
    41.  
    42.  
    43.  
    44.  
    45.     Vector3 screenPos;
    46.     Vector3 previousPosition;
    47.  
    48.     Score score;
    49.  
    50.     private void Start()
    51.     {
    52.         previousPosition = transform.position;
    53.         InvokeRepeating("spawnPrefab", spawnDelay, spawnDelay);
    54.         score = FindObjectOfType<Score>();
    55.     }
    56.  
    57.  
    58.     public void minusCount()
    59.     {
    60.         count--;
    61.     }
    62.  
    63.     public void minusBoosterCount()
    64.     {
    65.         boosterCount--;
    66.     }
    67.  
    68.     public void minusRainbowCount()
    69.     {
    70.         rainbowCount--;
    71.     }
    72.  
    73.     public void minusTntCount()
    74.     {
    75.         tntCount--;
    76.     }
    77.  
    78.     public void minusGhostCount()
    79.     {
    80.         ghostCount--;
    81.     }
    82.  
    83.     public void minusBarrierCount()
    84.     {
    85.         barrierCount--;
    86.     }
    87.  
    88.     public void addRainbowCount()
    89.     {
    90.         rainbowCount++;
    91.     }
    92.  
    93.     public void addBoosterCount()
    94.     {
    95.         boosterCount++;
    96.     }
    97.  
    98.     public void addTntCount()
    99.     {
    100.         tntCount++;
    101.     }
    102.  
    103.     public void addGhostCount()
    104.     {
    105.         ghostCount++;
    106.     }
    107.  
    108.     public void addBarrierCount()
    109.     {
    110.         barrierCount++;
    111.     }
    112.  
    113.     public int getRainbowCount()
    114.     {
    115.         return rainbowCount;
    116.     }
    117.  
    118.     public int getRainbowLimit()
    119.     {
    120.         return rainbowLimit;
    121.     }
    122.  
    123.  
    124.     private void Update()
    125.     {
    126.         if(count < 0)
    127.         {
    128.             count = 0;
    129.         }
    130.  
    131.         if(score != null)
    132.         {
    133.             if (score.getScore() > 150)
    134.             {
    135.                 tntSpawnChance = 0.6f;
    136.             }
    137.  
    138.             if(score.getScore() > 300)
    139.             {
    140.                 tntLimit = 2;
    141.             }
    142.  
    143.             if(score.getScore() > 100)
    144.             {
    145.                 barrierLimit = 2;
    146.             }
    147.         }    
    148.     }
    149.  
    150.  
    151.     void spawn(float minXPos, float maxXPos, float chance, GameObject prefab)
    152.     {
    153.         if (Random.value <= chance)
    154.         {
    155.             if(count < itemsPerScreen)
    156.             {
    157.                 Vector3 finalPos;
    158.                 float yPos = previousPosition.y + 10;
    159.  
    160.                 if (Random.value <= 0.5)
    161.                 {
    162.                     finalPos = new Vector3(Mathf.Clamp(screenPos.x + previousPosition.x, minXPos, maxXPos), yPos, transform.position.z);
    163.                 }
    164.                 else
    165.                 {
    166.                     finalPos = new Vector3(Mathf.Clamp(screenPos.x - previousPosition.x, minXPos, maxXPos), yPos, transform.position.z);
    167.                 }
    168.  
    169.                 GameObject stuff = Instantiate(prefab, finalPos, Quaternion.identity);
    170.                 previousPosition = new Vector3(Mathf.Clamp(stuff.transform.position.x, -2.22f, 2.22f), stuff.transform.position.y - yOffset, stuff.transform.position.z);
    171.  
    172.                 count++;
    173.             }        
    174.         }
    175.     }
    176.  
    177.     public void spawnPrefab()
    178.     {
    179.         screenPos = Camera.main.ViewportToWorldPoint(new Vector3(Mathf.Clamp(Random.Range(0.1f, 0.7f), -2.22f, 2.22f), Random.Range(1.1f, 2), 10));
    180.  
    181.         foreach (GameObject thing in prefabs)
    182.         {
    183.             if (count < itemsPerScreen)
    184.             {
    185.                 if (thing.GetComponent<Booster>() && boosterCount < boosterLimit)
    186.                 {
    187.                     spawn(-2.4f, 2.4f, boosterSpawnChance, thing);
    188.                 }
    189.  
    190.                 if (thing.GetComponent<CoinBooster>() && boosterCount < boosterLimit)
    191.                 {
    192.                     spawn(-2.3f, 1.1f, boosterWithCoinsSpawnChance, thing);
    193.                 }
    194.  
    195.                 if (thing.GetComponent<Coin>())
    196.                 {
    197.                     spawn(-2.4f, 2.4f, coinSpawnChance, thing);
    198.                 }
    199.  
    200.                 if (thing.GetComponent<Coins>())
    201.                 {
    202.                     spawn(-1.9f, 1.9f, multipleCoinsSpawnChance, thing);
    203.                 }
    204.  
    205.                 if (thing.GetComponent<RainbowBlock>() && rainbowCount < rainbowLimit)
    206.                 {
    207.                     spawn(-2.4f, 2.4f, rainbowDustSpawnChance, thing);
    208.                 }
    209.  
    210.                 if (thing.GetComponent<CoinRainbow>() && rainbowCount < rainbowLimit)
    211.                 {
    212.                     spawn(-1.8f, 1.8f, rainbowDustWithCoinsSpawnChance, thing);
    213.                 }
    214.  
    215.                 if (thing.GetComponent<CoinRainbow2>() && rainbowCount < rainbowLimit)
    216.                 {
    217.                     spawn(-3.8f, -0.04f, multipleCoinsSpawnChance, thing);
    218.                 }
    219.  
    220.                 if (thing.GetComponent<TNT>() && tntCount < tntLimit)
    221.                 {
    222.                     spawn(-2.4f, 2.4f, tntSpawnChance, thing);
    223.                 }
    224.  
    225.                 if (thing.GetComponent<Ghost>() && ghostCount < ghostLimit)
    226.                 {
    227.                     spawn(-2.4f, 2.4f, ghostSpawnChance, thing);
    228.                 }
    229.  
    230.                 if(thing.GetComponent<CubeBarrier>() && barrierCount < barrierLimit)
    231.                 {
    232.                     spawn(-2f, 1.6f, barrierSpawnChance, thing);
    233.                 }
    234.  
    235.                 if (thing.GetComponent<Barrier3>() && barrierCount < barrierLimit)
    236.                 {
    237.                     spawn(-2f, 0.63f, barrierSpawnChance, thing);
    238.                 }
    239.  
    240.  
    241.             }
    242.  
    243.             else
    244.                 break;
    245.         }
    246.  
    247.     }
     
  2. Xiromtz

    Xiromtz

    Joined:
    Feb 1, 2015
    Posts:
    65
    The code looks ok and since it's always the same Instantiate code, either ALL or NONE should be spawned, not only some.

    My conclusion would be that something is wrong with the prefab references in the Unity Editor itself. Maybe do some debugging and check which prefabs exactly are not working and then go from there?

    You could simply debug the value of the gameobject before or after Instantiate is called. Maybe some of your references are null?

    Also, did you check the hierarchy if the objects are created, but spawned off camera or are disabled?
     
  3. Xiromtz

    Xiromtz

    Joined:
    Feb 1, 2015
    Posts:
    65
    Alternatively, your ifs just aren't fired for some of your prefabs?
    Also, I don't think
    if(GetComponent<X>())
    works like that.
    You need to check for null:
    if (GetComponent<X>() != null)
    should be correct.

    Additionally,
    GetComponent<X>()
    only checks the components of the prefab gameobject, not its children. If you want to check for components in children you have to call
    GetComponentInChildren
     
  4. xXAl-HarereXx

    xXAl-HarereXx

    Joined:
    Aug 21, 2017
    Posts:
    101
    I did the != null thing and I did the debugging but nothing changed. I noticed that once the "Barrier" started spawning which is when score is > 100 all other things lowered. For example Ghost was only spawned 3 times while it supposed to have a 60% chance of spawning with 2 limits. Idk how..
    Capture.PNG
     
  5. Xiromtz

    Xiromtz

    Joined:
    Feb 1, 2015
    Posts:
    65
    Okay, so your problem has nothing to do with the Instantiate functionality of Unity, but instead is a problem within the logic of your game?
    You can't count on probability always working how you want, that's how randomness is supposed to be, unless you've tested it multiple times and the number is always so low?

    To be honest, I don't quite understand what your problem is, if your gameobject ARE spawning?

    It seems your question is a lot more specific and you might have to go into more detail on the logic of your game for me to be able to help you..
     
  6. Xiromtz

    Xiromtz

    Joined:
    Feb 1, 2015
    Posts:
    65
    I need to know what it is you are trying to achieve with your functions.
    SpawnPrefab
    is named in a way where I would assume it spawns a single prefab, but it simply loops through your whole list and spawns everything with those components?
     
  7. xXAl-HarereXx

    xXAl-HarereXx

    Joined:
    Aug 21, 2017
    Posts:
    101
    Found the problem :) It was in another script. The count of ghost wasn't going down when I take it. Thanks!