samedi 19 octobre 2019

How can I randomize the public Transform tilePrefab in my code?

So I want to make a map generator and have a prefab for a tile. But I want to make more tiles which it randomly chooses from.

How can I make it that tilePrefab is chosen randomly every time? From an array from prefabs.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MapGenerator : MonoBehaviour
{

    public Transform tilePrefab; //This is the part which I want to be random
    public Vector2 mapSize;

    private void Start()
    {
        GenerateMap();
    }
    public void GenerateMap()
    {
        for (int x = 0; x < mapSize.x; x++)
        {
            for (int y = 0; y < mapSize.y; y++)
            {
                Vector3 tilePosition = new Vector3(-mapSize.x / 2 + 0.5f 
+ x, 0,-mapSize.y / 2 + 0.5f + y);
                Transform newTile = Instantiate(tilePrefab, tilePosition, 
Quaternion.Euler(Vector3.right*360)) as Transform;
            }
        }
    }

}

I want that tilePrefab is choosen randomly every time a tile gets generated.




Aucun commentaire:

Enregistrer un commentaire