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. Dismiss Notice

2D enemy spawner acting weird

Discussion in 'Scripting' started by catkillgames, Nov 26, 2020.

  1. catkillgames

    catkillgames

    Joined:
    Nov 5, 2019
    Posts:
    2
    Im making a 2d side scroller game. and i need to make a spawner to spawn enemies infront of me.
    The spawning and all works great. but it doesnt randomly spawn an enemy. i am using a random value to spawn enemies. 2 have made 2 enemies. 1 single box. and 3 boxes stacked on eachother.
    But i does only spawn the 3 boxes, doesnt matter what i set the value on


    Im not the best at english but i hope you understand my problem.
    Triple boxes 10% chance to spawn
    single box 90% chance to spawn

    It doesnt give me a message in console too when it spawns something.
    (theres some random code btw in there that i need to remove)
     
  2. FlashMuller

    FlashMuller

    Joined:
    Sep 25, 2013
    Posts:
    449
    Random Value is only assigned once at there very beginneng and stays the same after that. That's why it only spawns the same. Have randValue = Random.value as the first line in your Spawnboxes() Function.

    EDIT: And post your code as text insidie Code-Tags instead of an image.
     
    catkillgames likes this.
  3. VishwasGagrani

    VishwasGagrani

    Joined:
    May 12, 2018
    Posts:
    81
    Put the line
    Code (CSharp):
    1. randValue = Random.value
    inside the method Spawnboxes()
     
  4. catkillgames

    catkillgames

    Joined:
    Nov 5, 2019
    Posts:
    2
    Thanks! this really helped