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

Instantiate issues

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

  1. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    I am using Perlin Noise to generate a quasi-random 2D terrain. I am in the very beginning and as of right now if the spot on the map if >= .6 then a wall is created, otherwise ground is created. My problem comes with trying to update the terrain, I can't figure out how to set the tiles parent as it is being Instantiated or how to delete them after the map is updated.

    Tile Class:
    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class PerlinDisplay : MonoBehaviour
    6. {
    7.     public Transform chunk;
    8.  
    9.     public void UpdateTiles(float[,] noiseMap, Transform[] tiles)
    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] >= .6)
    19.                 {
    20.                     GameObject tileObj = Instantiate(tiles[0], new Vector3(x, y, 0), new Quaternion(0, 0, 0, 0)) as GameObject;
    21.                     tileObj.transform.parent = chunk.transform; //doesn't work
    22.                 }
    23.                 else
    24.                 {
    25.                     GameObject tileObj = Instantiate(tiles[1], new Vector3(x, y, 0), new Quaternion(0, 0, 0, 0)) as GameObject;
    26.                     tileObj.transform.parent = chunk.transform; //doesn't work
    27.                 }
    28.             }
    29.         }
    30.     }
    31. }
    32.  
    Generator Class:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class PerlinGenerator : MonoBehaviour
    5. {
    6.     public MapMods mapModifiers;
    7.  
    8.     public bool autoUpdate;
    9.  
    10.     public void GenerateMap()
    11.     {
    12.         float[,] noiseMap = PerlinValues.GeneratePerlinValues(mapModifiers.width, mapModifiers.height, mapModifiers.scale);
    13.  
    14.         PerlinDisplay disp = FindObjectOfType<PerlinDisplay>();
    15.         disp.UpdateTiles(noiseMap, mapModifiers.tiles);
    16.     }
    17. }
    18.  
    19. [System.Serializable]
    20. public class MapMods
    21. {
    22.     public int width;
    23.     public int height;
    24.     public float scale;
    25.  
    26.     public Transform[] tiles;
    27. }
    Noise Generator Class:

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public static class PerlinValues
    5. {
    6.     public static float[,] GeneratePerlinValues(int chunkWidth, int chunkHeight, float scale)
    7.     {
    8.         float[,] noiseMap = new float[chunkWidth, chunkHeight];
    9.  
    10.         if (scale <= 0)
    11.             scale = .001f;
    12.  
    13.         for (int y = 0; y < chunkHeight; y++)
    14.         {
    15.             for (int x = 0; x < chunkWidth; x++)
    16.             {
    17.                 float sampleX = x / scale;
    18.                 float sampleY = y / scale;
    19.  
    20.                 float perlinNoise = Mathf.PerlinNoise(sampleX, sampleY);
    21.                 noiseMap[x, y] = perlinNoise;
    22.             }
    23.         }
    24.  
    25.         return noiseMap;
    26.     }
    27. }
    Thank you for reading, have a nice day :)
     
  2. grimofdoom

    grimofdoom

    Joined:
    Sep 6, 2013
    Posts:
    168
    You can set a parent using 'tileobj.transform.setparent(parent.transform)', parent being another GameObject (such as en empty GameObject)

    And if you want a quick "reset" of all the blocks, you can add them into a list, and then "foreach" through it to destroy every item. and then make that list a new list.
    (naming scheme is something I use personally, such as 'mass' and 'particle')

    Code (CSharp):
    1.  
    2. public class Example : MonoBehaviour {
    3.     public List<GameObject> mass;
    4.     public GameObject parent;
    5.     public GameObject tileObj;
    6.  
    7.     void Start(){
    8.         mass = new List<GameObject> ();
    9.     }
    10.  
    11.     public void CreateFloor(){
    12.         for (int h = 0; h < 10; h++) {
    13.             for (int w = 0; w < 10; w++) {
    14.                 GameObject temp = Instantiate (tileObj, new Vector3 (h, 0, w), Quaternion.identity) as GameObject;
    15.                 temp.transform.SetParent (parent.transform);
    16.                 mass.add(temp);
    17.             }
    18.         }
    19.     }
    20.  
    21.     public void DeleteFloor(){
    22.         foreach (GameObject particle in mass) {
    23.             Destroy (particle);
    24.         }
    25.         mass = new List<GameObject> ();
    26.     }
    27. }
     
  3. Browdaddy96

    Browdaddy96

    Joined:
    Aug 27, 2015
    Posts:
    82
    Sorry for the late reply but thank you very much :)