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

Randomize

Discussion in 'Scripting' started by Milambur88, Feb 4, 2020.

  1. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    HI,

    not sure what i have missed but I cant seem to get my randomize to loop every 2 seconds.

    its spread across 2 scripts (sorry if the terminology is not correct still learning)
    1)
    Code (CSharp):
    1. world.RandomizeTiles();
    2.        
    3.     }
    4.  
    5.     float randomizeTileTimer = 2f;
    6.  
    7.     // Update is called once per frame
    8.     void update() {
    9.         randomizeTileTimer -= Time.deltaTime;
    10.  
    11.         if(randomizeTileTimer < 0) {
    12.             world.RandomizeTiles();
    13.             randomizeTileTimer = 2f;
    14.         }
    2)
    Code (CSharp):
    1. public void RandomizeTiles() {
    2.         Debug.Log("RandomizeTiles");
    3.         for (int x = 0; x < width; x++) {
    4.             for (int y = 0; y < height; y++) {
    5.  
    6.                 if(Random.Range(0, 2) == 0) {
    7.                     tiles[x, y].Type = Tile.TileType.Empty;
    8.                 }
    9.                 else {
    10.                     tiles[x, y].Type = Tile.TileType.Floor;
    11.                 }
    12.             }
    13.  
    14.         }
    15.        
    16.  
    17.     }
     
  2. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,151
    Update is a Unity magic method, which means it needs to start with a capital letter.

    Update and not update.

    Also note this is good practice anyways. Methods should start with a capital letter anyways, but it doesn't hurt stuff you write. It does keep Unity from running it though as it should.
     
    Milambur88 likes this.
  3. Milambur88

    Milambur88

    Joined:
    Jan 15, 2020
    Posts:
    28
    Thank you!!!