Search Unity

Bug Cellular automata algorithm generates every tile as black

Discussion in 'Scripting' started by kineticcrusher, Jun 10, 2020.

  1. kineticcrusher

    kineticcrusher

    Joined:
    Aug 17, 2019
    Posts:
    27
    I'm currently following Sebastian Lague's tutorial on cellular automata...

    ...in order to generate a map with scattered islands.

    In the video, he uses gizmos to draw the map, but I would like an array of cube GameObjects to take their place instead.

    The original code used to draw the gizmos, right from the video, was

    Code (CSharp):
    1. private void OnDrawGizmos()
    2.     {
    3.         if (map != null)
    4.         {
    5.             for (int x = 0; x < width; x++)
    6.             {
    7.                 for (int y = 0; y < height; y++)
    8.                 {
    9.                     Gizmos.color = (map[x, y] == 1) ? Color.black : Color.white;
    10.                     Vector3 pos = new Vector3(-width / 2 + x + .5f, 0, -height / 2 + y + .5f);
    11.                     Gizmos.DrawCube(pos, Vector3.one);
    12.                 }
    13.             }
    14.         }
    15.     }
    In order to change it from using gizmos to using GameObjects, I tried this:

    Code (CSharp):
    1. private void DrawMap()
    2.     {
    3.         if (map != null)
    4.         {
    5.             for (int x = 0; x < width; x++)
    6.             {
    7.                 for (int y = 0; y < height; y++)
    8.                 {
    9.                     var tileRenderer = groundTile.GetComponent<Renderer>();
    10.                     if (map[x, y] == 1)
    11.                     {
    12.                         tileRenderer.sharedMaterial.color = Color.black;
    13.                     }
    14.                     else if (map[x, y] == 0)
    15.                     {
    16.                         tileRenderer.sharedMaterial.color = Color.white;
    17.                     }
    18.                     Instantiate(groundTile, new Vector3(-width / 2 + x + .5f, 0, -height / 2 + y + .5f), Quaternion.identity);
    19.                 }
    20.             }
    21.         }
    22. }
    When I run the program, the array of GameObjects generates correctly (I've already set up a reference to the groundTile prefab in the Inspector), but it generates every single one as black.

    If needed, I can post the entire program as a means of thorough debugging.

    How can I fix my problem?
     
  2. ThermalFusion

    ThermalFusion

    Joined:
    May 1, 2011
    Posts:
    906
    That's because you're using Renderer.sharedMaterial. You're manipulating just one original material, and all your instances are using this.
    What you seem to want is just regular Renderer.material.
    I'd suggest just replacing the material from a fixed set though, otherwise you would end up with a unique material for each object, guessing you don't need that amount of individuality.
     
    kineticcrusher likes this.
  3. jvo3dc

    jvo3dc

    Joined:
    Oct 11, 2013
    Posts:
    1,520
    Note the memory leak introduced by just adjusting Renderer.material. You have to manually destroy any materials that you don't use anymore. (Replacing from a fixed set significantly reduces the problem.)
     
    kineticcrusher likes this.
  4. benthroop

    benthroop

    Joined:
    Jan 5, 2007
    Posts:
    262
    Look into DrawMeshInstanced and use MaterialPropertyBlocks. If drawing is really your long pole you can save a lot with these methods.