- Home /
2D Building Script Help Needed
C# I am using 3d Cubes in this 2d sandbox game because 2d Quads didn't work.
using System.Collections;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
public Text InventoryText, SelectionText;
int[] counts = new int[7];
int selectedTile;
string[] names = new string[] { "Dirt", "Turf", "Stone", "Iron", "Ramp", "Diamond", "Solid Fuel" };
public GameObject[] tiles = new GameObject[7];
void Update()
{
InventoryText.text = "Dirt: " + counts[0] + "Turf: " + counts[1] + "Stone: " + counts[2] + "Iron: " + counts[3] + "Ramps: " + counts[4] + "Diamond: " + counts[5] + "Solid Fuel: " + counts[6];
if (selectedTile < 0)
{
selectedTile = counts.Length - 1;
}
if (selectedTile > counts.Length - 1)
{
selectedTile = 0;
}
if (Input.GetKeyDown(KeyCode.UpArrow))
{
selectedTile++;
}
if (Input.GetKeyDown(KeyCode.DownArrow))
{
selectedTile--;
}
SelectionText.text = names[selectedTile];
if (Input.GetMouseButtonDown(1)){
if (counts[selectedTile] > 0)
{
Vector3 mousepos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
Vector3 placepos = new Vector3(Mathf.Round(mousepos.x), Mathf.Round(mousepos.y), 0f);
if (Physics2D.OverlapCircleAll(placepos, 0.25f).Length == 0)
{
GameObject newTile = Instantiate(tiles[selectedTile], placepos, Quaternion.identity) as GameObject;
counts[selectedTile]--;
}
}
}
}
public void Add(int tileType, int count){
counts[tileType] += count;
}
}
Thanks :)
Comment