Using Buttons to create units only when selecting the barracks.
Hi i need a little help here...
basically i need to click on a object like the (barracks)
then have it change the buttons on the hud to what ever i place them inside to be..
for example button 1 will spawn a sniper button 2 will spawn a rocketguy etc
at the moment i have my barracks spawning the two units by pressing keys on the keyboard..
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class Buildings : MonoBehaviour
{
//Buttons for Hud
public Rigidbody BuildQPrefab1, BuildQPrefab2;
public Transform spawn;
//drag Selection
public bool selected = false;
//private Vector3 moveToDest = Vector3.zero;
public float floorOffSet = 1;
//Click Select
private bool selectionByClick = false;
// Update is called once per frame
private void Update ()
{
HudButtonsUsage ();
//Place Layer Unit Control Here.
if (Input.GetMouseButton (0))
{
if (!selectionByClick)
{
Vector3 camPos = Camera.main.WorldToScreenPoint (transform.position);
camPos.y = CamOP.InvertMouseY(camPos.y);
selected = CamOP.selection.Contains (camPos);
}
if (selected)
gameObject.GetComponentInChildren<Projector>().enabled = true;
else
gameObject.GetComponentInChildren<Projector>().enabled = false;
}
}
//Function for Spawning
private void HudButtonsUsage()
{
//This Must be the first Button on the HUD
if (Input.GetKeyUp ("t"))
{
Instantiate(BuildQPrefab1, spawn.position, spawn.rotation);
}
//This Must be the Second Button on the HUD
if (Input.GetKeyUp ("y"))
{
Instantiate(BuildQPrefab2, spawn.position, spawn.rotation);
}
}
//Function for SimpleMouseClick
private void OnMouseDown()
{
selectionByClick = true;
selected = true;
}
private void OnMouseUp()
{
if (selectionByClick)
selected = true;
selectionByClick = false;
}
}
This is the button setup I Currently have..
and this is kind of what it looks like..
Your answer
Follow this Question
Related Questions
NetworkServer.Spawn() only on Server (with registered prefabs) [Unity 5.2.3f1] 0 Answers
Can i add a time var to my spawnInterval for increasing spawing over time? 0 Answers
Changing The Size Of NetworkManagerHUD 0 Answers
End Game (Stop spawning) with OnTriggerEnter or OnCollisionEnter? 0 Answers
Problem instantiating objects one after another with mouse click 1 Answer