Change old GUI to new UI button not creating objects
I am trying to make an RTS game and want the list of objects that can be created to display when the user clicks on the build building (Construction Yard, Barracks, War Factory etc).I have followed a tutorial to create a menu based on an array of gameobjects that creates an object when clicked using the following code.
void OnGUI() {
for (int i = 0; i <buildings.Length; i ++) { //buildings is the array set in the inspector
if (GUI.Button(new Rect(Screen.width/20,Screen.height/15 + Screen.height/12 * i,100,30), buildings[i].name)) {
buildingPlacement.SetItem(buildings[i]);
}
}
}
I tried to convert this code to the new UI by creating the canvas and text buttons in script but have been unable to get the buttons to create objects on click as the old code does.
My new code is
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class BuildingManager : MonoBehaviour {
public GameObject[] buildings;
private BuildingPlacement buildingPlacement;
public GameObject guiCanvas;
public GameObject guiButton;
private GameObject mainUI;
// Use this for initialization
void Start () {
buildingPlacement = GetComponent<BuildingPlacement>();
}
// Update is called once per frame
void Update () {
}
public void CreateTextUI ()
{
HideMainUI(); //Hide the old UI
guiCanvas = new GameObject();
guiCanvas.name = "BuildCanvas";
guiCanvas.AddComponent<Canvas>(); //Create a new Canvas and add settings
Canvas buildCanvas = guiCanvas.GetComponent<Canvas>();
buildCanvas.renderMode = RenderMode.ScreenSpaceOverlay;
for (int i = 0; i <buildings.Length; i++) {
guiButton = new GameObject();
guiButton.name = buildings[i].name + "Button"; //Create a button
guiButton.transform.parent = guiCanvas.transform; //Add the button to the Canvas as a child
guiButton.AddComponent<RectTransform>(); //Add a RectTransform to move the button
guiButton.AddComponent<Text>(); //Add a Text Object to display text
guiButton.AddComponent<Button>(); //Add a Button Component To the GameObject
Text textComponent = guiButton.GetComponent<Text>();
Font myFont = Resources.Load<Font>("Jupiter"); //Get the font Resource from a Resources folder
textComponent.font = myFont; //Set the Font
textComponent.text = buildings[i].name; //Set the Text
textComponent.rectTransform.position = new Vector2(guiCanvas.transform.position.x/8f, guiCanvas.transform.position.y/15f
+ guiCanvas.transform.position.y/8 * i);
//Move the position of the Button
textComponent.fontSize = 30;
RectTransform rt = guiButton.GetComponent<RectTransform>();
rt.sizeDelta = new Vector2(150, 35);
Button buttonComponent = guiButton.GetComponent<Button>(); //add a button component to the text object
buttonComponent.onClick.AddListener(() => buildingPlacement.SetItem(buildings[i]));// this is the code I found from other examples which doesn't work
}
}
void HideMainUI ()
{
mainUI = GameObject.Find("MainUICanvas");
mainUI.SetActive(false);
}
}
Any help would be greatly appreciated
EDIT: I solved one of the problems, As I hadn't added a Graphic Raycast to the canvas it wasn't even detecting the button had been pressed. It still isn't creating the item onClick but if I pause the game and add the function to the button onClick in the inspector it works fine
Andy
Your answer

Follow this Question
Related Questions
Expose method with Enum parameter to editor 0 Answers
How to do Unlockable Items for a Character Collection Clicker Game? How to disable buttons? 0 Answers
Trigger when holding key 1 Answer
How to change HotKeys for UI Navigation? 0 Answers
With the new unity input system. How do I setup UI buttons like in the old system. 0 Answers