Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
1
Question by andy_coult · Mar 30, 2016 at 01:14 PM · button trigger events

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

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Figuring out which function to call on button click 0 Answers

Cannot assign variable agents for clicking a button 0 Answers

How do I can use Arduino push button trigger unity UI button (on click ()) , no direct active effect? code can function but it had duplicate code and not the result that I want. 0 Answers

Dymanic buttons with different openURL links 0 Answers

multiple buttons activating at the same time, I'm at my wits end 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges