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
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

[Solved]Pre-running instaciating issues

Discussion in 'Scripting' started by Browdaddy96, Mar 27, 2016.

  1. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    I am trying to create a list of GameObjects that are instantiated prior to running. I think you will understand better if you read the script:

    Code (CSharp):
    1. public List<GameObject> chunk = new List<GameObject>();
    2.  
    3.     public void CreateChunk(float[,] noiseMap, Transform[] tileSet)
    4.     {
    5.         int width = noiseMap.GetLength(0);
    6.         int height = noiseMap.GetLength(1);
    7.  
    8.         for (int y = 0; y < height; y++)
    9.         {
    10.             for (int x = 0; x < width; x++)
    11.             {
    12.                 if (noiseMap[x, y] <= .6f)
    13.                 {
    14.                     GameObject tile = Instantiate(tileSet[0], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
    15.                     chunk.Add(tile);
    16.                 }
    17.                 else
    18.                 {
    19.                     GameObject tile = Instantiate(tileSet[1], new Vector3(x, y, 0), Quaternion.identity) as GameObject;
    20.                     chunk.Add(tile);
    21.                 }
    22.  
    23.             }
    24.         }
    25.     }
    26.  
    27.     public void DeleteChunk(List<GameObject> chunkToBeDeleted)
    28.     {
    29.         foreach(GameObject tile in chunk)
    30.         {
    31.             Destroy(tile);
    32.             chunk.Remove(tile);
    33.             chunk = new List<GameObject>();
    34.         }
    35.     }
    Create and Delete chunk are called in a ma generator class.
     
  2. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    What problem are you having?
     
  3. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    The list is creating all of the spots, if the map is 10 wide and 10 tall there is 100 slots, but the all show "none (GameObject)"
     
  4. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    I acualy made it work by doing this:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class PerlinDisplay : MonoBehaviour
    6. {
    7.     public List<GameObject> chunk = new List<GameObject>();
    8.  
    9.     public void CreateChunk(float[,] noiseMap, Transform[] tileSet)
    10.     {
    11.         int width = noiseMap.GetLength(0);
    12.         int height = noiseMap.GetLength(1);
    13.  
    14.         for (int y = 0; y < height; y++)
    15.         {
    16.             for (int x = 0; x < width; x++)
    17.             {
    18.                 if (noiseMap[x, y] <= .6f)
    19.                 {
    20.                     Transform tile = Instantiate(tileSet[0], new Vector3(x, y, 0), Quaternion.identity) as Transform;
    21.                     chunk.Add(tile.gameObject);
    22.                 }
    23.                 else
    24.                 {
    25.                     Transform tile = Instantiate(tileSet[1], new Vector3(x, y, 0), Quaternion.identity)as Transform;
    26.                     chunk.Add(tile.gameObject);
    27.                 }
    28.  
    29.             }
    30.         }
    31.     }
    32.  
    33.     public void DeleteChunk()
    34.     {
    35.         foreach(GameObject gameObj in chunk)
    36.         {
    37.             DestroyImmediate(gameObj);
    38.             chunk = new List<GameObject>();
    39.         }
    40.     }
    41. }
    42.  
    for anyone who might want this :p
     
  5. SubZeroGaming

    SubZeroGaming

    Joined:
    Mar 4, 2013
    Posts:
    1,008
    Oh, because you were using a Transform for Tile, but adding it to a gameObject list.

    Yeah, for future reference, the types have to match. Can't add a transform to a game object list.
     
  6. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Yep, thank you for the help.