How do i make a realtime tiling??Help
Hello im trying to make a real time([ExecuteInEditMode]) tile maker that a public float can be edited in the inspector to increase and decrease amount of TIles but i cant seem to delete the previous 3Dobjects if you know what i mean
using UnityEngine;
using System.Collections;
[ExecuteInEditMode]
public class floor : MonoBehaviour
{
public Transform Tile;
public float TslotsX;
public float TslotsY;
void FixedUpdate()
{
for (int x = 0; x < TslotsX; x++)
{
for (int y = 0; y < TslotsZ; y++)
{
DestroyImmediate(prefab);
Instantiate(Tile, new Vector3(x * 1.0f, 0, y * 1.0f), Quaternion.identity);
}
}
}
}
Why you can't delete them? Store them in a List and delete if you need to.
Btw, don't use FixedUpdate() for such things. FixedUpdate is for physics-related things. Don't call anything each frame (using Update()) or every physics frame (using FixedUpdate()) if you don't need it to be done every frame.
Answer by incorrect · Jan 20, 2016 at 06:44 AM
using UnityEngine;
using System.Collections;
public class TilePlacer : MonoBehaviour
{
public GameObject TilePrefab;
[SerializeField]
private float TslotsX = 1f;
[SerializeField]
private float TslotsY = 1f;
[SerializeField]
private float tileStep = 1f;
private void RemoveTiles()
{
GameObject[] oldTilesArray = new GameObject[transform.childCount];
//Get an array of children; you can't destroy them by DestroyImmediate (transform.GetChild(i).gameObject) because their indexes will change every time you are destroying one
for(int i = 0; i < transform.childCount; i++)
oldTilesArray[i] = transform.GetChild(i).gameObject;
//Destroy them
foreach(GameObject oldTile in oldTilesArray)
DestroyImmediate (oldTile);
}
[ContextMenu("Place Tiles")]
private void PlaceTiles()
{
//remove old tiles if there are any
RemoveTiles();
//Place new ones
for (int x = 0; x < TslotsX; x++)
{
for (int y = 0; y < TslotsY; y++)
{
//Instantiate new tile and save a reference to it's Transform component
GameObject tileInstance = Instantiate(TilePrefab, new Vector3(x * tileStep, 0, y * tileStep), Quaternion.identity) as GameObject;
//Get this tile's transform component
Transform tileTransform = tileInstance.GetComponent<Transform>();
//Make this new tile a child of TilePlacer object for easier management
tileTransform.parent = transform;
}
}
}
}
Your answer
Follow this Question
Related Questions
Bizzare error with for loop and tex2D in HLSL shader 1 Answer
Counting the number of tiles in a path 0 Answers
Nav mesh stopping when on new tile 0 Answers
Loop function isn't updating 0 Answers
How to shoot spider man webs? 1 Answer