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

Resolved Prefabs not spawning

Discussion in 'Scripting' started by nsteele1031, Jan 10, 2022.

  1. nsteele1031

    nsteele1031

    Joined:
    Jan 7, 2022
    Posts:
    18
    I am using prefabs to spawn multiable trees at random places. I put in the gameobject in unity and there's no error code
    Code (CSharp):
    1. public class Spawning : MonoBehaviour
    2. {
    3.     public GameObject tree;
    4.     private int loop = 10;
    5.     // Start is called before the first frame update
    6.     void Start()
    7.     {
    8.        
    9.     }
    10.  
    11.     // Update is called once per frame
    12.     void Update()
    13.     {
    14.         if (loop < 1)
    15.         {
    16.             Instantiate(tree, new Vector3(Random.Range(-24, 25), 1, Random.Range(-24, 25)), new Quaternion(0, 0, 0, 0));
    17.             loop -= 1;
    18.         }
    19.     }
    20. }
     
  2. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    put the loop -= 1; out of the if statement
     
  3. RadRedPanda

    RadRedPanda

    Joined:
    May 9, 2018
    Posts:
    1,593
    I think what he wants is for it to be
    loop > 1
    instead of that.
     
    D12294 and Kurt-Dekker like this.
  4. alexeu

    alexeu

    Joined:
    Jan 24, 2016
    Posts:
    257
    yes indeed. but the main problem is that what is into the if statment can never happens because loop will
    never be < 1 while loop -= 1; is into the if
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,756
  6. nsteele1031

    nsteele1031

    Joined:
    Jan 7, 2022
    Posts:
    18
    Then what can i do to make the if statement loop threw the amount of times
     
  7. nsteele1031

    nsteele1031

    Joined:
    Jan 7, 2022
    Posts:
    18
    OHOHOHOH i seem my mistake now
    thank you guys
     
  8. nsteele1031

    nsteele1031

    Joined:
    Jan 7, 2022
    Posts:
    18
    its funny seeing such a simple mistake