- Home /
Button does not display, need urgent help!
I am following the BurgZergArcade Hack and Slash tutorials and I had an annoying issue:
There is an Equipment tab in the character window, which shows you what items you have equipped from your inventory. It is supposed to show an empty GUI.Label when an item is not equipped, and it is by all means supposed to show a GUI.Button when an item is equipped. In my case, it does not, and the double-clicking on an item in the inventory does not even do anything.
I have purchased all the C# scripts from BurgZergArcade.com, and they are perfect and do not have any errors. Here are both the GUI script and the character script if it will help:
CHARACTER SCRIPT:
///
/// PlayerCharacter.cs /// Oct 20, 2010 /// /// This script controls the users ingame character. /// /// Make sure this script is attached to the character prefab ///using UnityEngine; using System.Collections.Generic;
[AddComponentMenu("Hack And Slash Tutorial/Player/Player Character Stats")] public class PlayerCharacter : BaseCharacter { public static GameObject[] _weaponMesh;
private static List<Item> _inventory = new List<Item>(); public static List<Item> Inventory { get{ return _inventory; } } private static Item _equipedWeapon; public static Item EquipedWeapon { get { return _equipedWeapon; } set { _equipedWeapon = value; HideWeaponMeshes(); if(_equipedWeapon == null) return; switch(_equipedWeapon.Name) { case "7 branch sword": _weaponMesh[0].active = true; break; case "aqua dagger": _weaponMesh[1].active = true; break; case "aqua saber": _weaponMesh[2].active = true; break; case "barbarian king axe": _weaponMesh[3].active = true; break; case "blackblade dagger": _weaponMesh[4].active = true; break; case "blood sword": _weaponMesh[5].active = true; break; case "crystal hammer": _weaponMesh[6].active = true; break; case "crystal sword": _weaponMesh[7].active = true; break; case "darkness": _weaponMesh[8].active = true; break; case "diamond hammer": _weaponMesh[9].active = true; break; case "dragontooth spear": _weaponMesh[10].active = true; break; case "emperor's oath": _weaponMesh[11].active = true; break; case "fire claws": _weaponMesh[12].active = true; break; case "fire knuckles": _weaponMesh[13].active = true; break; case "fire scythe": _weaponMesh[14].active = true; break; case "fireseed blade": _weaponMesh[15].active = true; break; case "flametwirl blade": _weaponMesh[16].active = true; break; case "frost knuckles": _weaponMesh[17].active = true; break; case "frosty battering mace": _weaponMesh[18].active = true; break; case "frosty skinner": _weaponMesh[19].active = true; break; case "gladius": _weaponMesh[20].active = true; break; case "golden claw": _weaponMesh[21].active = true; break; case "great flametwirl blade": _weaponMesh[22].active = true; break; case "great frosty sword": _weaponMesh[23].active = true; break; case "great icefang": _weaponMesh[24].active = true; break; case "great lightbane": _weaponMesh[25].active = true; break; case "great shadow twister": _weaponMesh[26].active = true; break; case "great shadowleak blade": _weaponMesh[27].active = true; break; case "great emerald sword": _weaponMesh[28].active = true; break; case "icefang": _weaponMesh[29].active = true; break; case "lavarock blade": _weaponMesh[30].active = true; break; case "lightbane": _weaponMesh[31].active = true; break; case "lightning glove": _weaponMesh[32].active = true; break; case "mithril knuckles": _weaponMesh[33].active = true; break; case "plasma saber": _weaponMesh[34].active = true; break; case "punishment": _weaponMesh[35].active = true; break; case "rainbow sword": _weaponMesh[36].active = true; break; case "shadow scythe": _weaponMesh[37].active = true; break; case "shadowleak blade": _weaponMesh[38].active = true; break; case "shadowtwister blade": _weaponMesh[39].active = true; break; case "silver sword": _weaponMesh[40].active = true; break; case "energy star saber": _weaponMesh[41].active = true; break; case "star saber": _weaponMesh[42].active = true; break; case "sun sword": _weaponMesh[43].active = true; break; case "thief's knife": _weaponMesh[44].active = true; break; case "tombstoner": _weaponMesh[45].active = true; break; default: break; } } } public override void Awake() { base.Awake(); /************************** Check out tutorial #140 and #141 to see how we got this weaponMount **************************/ Transform weaponMount = transform.Find("playermove/Armature/pelvis/spine_01/Spine/Chest/DLT-upper_arm_R/upper_arm_R/forearm_R/hand_R/RH Weapon Mount"); if(weaponMount == null) { Debug.LogWarning("We could not find the weapon mount"); return; } int count = weaponMount.GetChildCount(); _weaponMesh = new GameObject[count]; for(int cnt = 0; cnt < count; cnt++) { _weaponMesh[cnt] = weaponMount.GetChild(cnt).gameObject; } HideWeaponMeshes(); } //we do not want to be sending messages out each frame. We will be moving this out when we get back in to combat void Update() { Messenger<int, int>.Broadcast("player health update", 80, 100, MessengerMode.DONT_REQUIRE_LISTENER); } private static void HideWeaponMeshes() { for(int cnt = 0; cnt < _weaponMesh.Length; cnt++) { _weaponMesh[cnt].active = false; // Debug.Log(_weaponMesh[cnt].name); } } }
GUI SCRIPT
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.Collections.ObjectModel; using System.Collections.Specialized;
[AddComponentMenu("Hack And Slash Tutorial/GUI/HUD")] public class MyGUI : MonoBehaviour { public GUISkin mySkin; //create a public variable for us to reference our custom guiskin
public string emptyInventorySlotStyle; //the style name for an epty inventory slot public string closeButtonStyle; //the style name for a close button public string inventorySlotCommonStlye; //the stlye name for a common item public float lootWindowHeight = 200; //define how tall our loot window will be public float buttonWidth = 40; //define the width of all of the buttons we will be using for items public float buttonHeight = 40; //define the height of all of the buttons we wll be using for items public float closeButtonWidth = 20; //the width of a close button (small x in a corner) public float closeButtonHeight = 20; //the heght of a close button (small x in the corner) // private List<Item> _lootItems; private float _offset = 10; //the default offset that is going to be used when spacing gui elements private string _toolTip = ""; //the tooltip to display #region Loot Window Variables /*************************************************** * Loot window variables * * These variables are only used fot the loot window ***************************************************/ private bool _displayLootWindow = false; //toggle to control if the loot window should be displayed or not private const int LOOT_WINDOW_ID = 0; //the unique id of the loot window private Rect _lootWindowRect = new Rect(0,0,0,0); //the starting location of the loot window private Vector2 _lootWindowSlider = Vector2.zero; //the scrollview of the loot window public static Chest chest; //the reference to the lootable object that we are currently using #endregion #region Inventory Window Variables /******************************************************* * Inventory window variables * * These variables are only used fot the inventory window *******************************************************/ private bool _displayInventoryWindow = false; //toggle the display of the inventory window. private const int INVENTORY_WINDOW_ID = 1; //the unique id of the loot window private Rect _inventoryWindowRect = new Rect(250, 30, 410, 265); //the starting location of the inventory window private int _inventoryRows = 6; //the number of rows of items in our inventory private int _inventoryCols = 10; //the number of columns in our inventory private float _doubleClickTimer = 0; //allow us to track when clicks happen private const float DOUBLE_CLICK_TIMER_THRESHHOLD = 0.5f; //the default threshhold for how fast a click has to occur be be considered a double click private Item _selectedItem; //the item that we clicked on #endregion #region Character Window Variables /******************************************************* * Character window variables * * These variables are only used fot the character window *******************************************************/ private bool _displayCharacterWindow = false; //toggle the display of the character window private const int CHARACTER_WINDOW_ID = 2; //the unique id of the character window private Rect _characterWindowRect = new Rect(250, 30, 800, 500); //the default placement of the character window private int _characterPanel = 0; //the current tab we have selected from toolbar private string[] _characterPanelNames = new string[] {"Equipment", //the name of the tabs in the toolbar "Attributes", "Skills" }; #endregion /// <summary> /// Start this instance. /// /// This function is called before the first Update is called. Use this function to set tings up before we start. /// </summary> void Start() { // _lootItems = new List<Item>(); } /// <summary> /// Raises the enable event. /// /// Make sure you add all of the listeners that are going to be needed for this script /// </summary> private void OnEnable() { // Messenger<int>.AddListener("PopulateChest", PopulateChest); Messenger.AddListener("DisplayLoot", DisplayLoot); //display the loot window Messenger.AddListener("ToggleInventory", ToggleInventoryWindow); //display the inventory Messenger.AddListener("ToggleCharacterWindow", ToggleCharacterWindow); //display the character window Messenger.AddListener("CloseChest", ClearWindow); //close the loot window. } /// <summary> /// Raises the disable event. /// /// Make sure you add the remove listeners for all of the events that you are listenning for /// </summary> private void OnDisable() { // Messenger<int>.RemoveListener("PopulateChest", PopulateChest); Messenger.RemoveListener("DisplayLoot", DisplayLoot); //display the loot window Messenger.RemoveListener("ToggleInventory", ToggleInventoryWindow); //display the inventory window Messenger.RemoveListener("ToggleCharacterWindow", ToggleCharacterWindow); //display the character window Messenger.RemoveListener("CloseChest", ClearWindow); //close the loot window } /// <summary> /// Raises the GUI event. /// /// This function draws all of the GUI elements on the screen. This functions can be called more then once per frame. /// </summary> void OnGUI() { //define the skin that we are going to be using for our window GUI.skin = mySkin; //dispplay the character window if we are set to. if(_displayCharacterWindow) _characterWindowRect = GUI.Window(CHARACTER_WINDOW_ID, _characterWindowRect, CharacterWindow, "Character"); //dispplay the inventory window if we are set to. if(_displayInventoryWindow) _inventoryWindowRect = GUI.Window(INVENTORY_WINDOW_ID, _inventoryWindowRect, InventoryWindow, "Inventory"); //dispplay the loot window if we are set to. if(_displayLootWindow) _lootWindowRect = GUI.Window(LOOT_WINDOW_ID, new Rect(_offset, Screen.height - (_offset + lootWindowHeight), Screen.width - (_offset * 2), lootWindowHeight), LootWindow, "Loot Window", "box"); //display the tooltip that we have DisplayToolTip(); } /// <summary> /// Display the loot window that will display all of the loot that is in the object we are looting. /// </summary> /// <param name='id'> /// Identifier for the window we are going to use. /// </param> private void LootWindow(int id) { //define the skin that we are going to be using for the elements in the loot window GUI.skin = mySkin; //add a close button to the window if(GUI.Button(new Rect(_lootWindowRect.width - _offset * 2, 0, closeButtonWidth, closeButtonHeight), "x", closeButtonStyle)) ClearWindow(); if(chest == null) return; if(chest.loot.Count == 0) { ClearWindow(); return; } // _lootWindowSlider = GUI.BeginScrollView(new Rect(_offset * .5f, 15, _lootWindowRect.width - _offset, 70), _lootWindowSlider, new Rect(0, 0, (_lootItems.Count * buttonWidth) + _offset, buttonHeight + _offset)); //create a scroll view for our loot window _lootWindowSlider = GUI.BeginScrollView(new Rect(_offset * .5f, 15, _lootWindowRect.width - _offset, 70), _lootWindowSlider, new Rect(0, 0, (chest.loot.Count * buttonWidth) + _offset, buttonHeight + _offset)); //iterate though the items in the lootable object and display them in the scroll view as a styled button for(int cnt = 0; cnt < chest.loot.Count; cnt++) { if(GUI.Button(new Rect(_offset * .5f + (buttonWidth * cnt), _offset, buttonWidth, buttonHeight), new GUIContent(chest.loot[cnt].Icon , chest.loot[cnt].ToolTip()), inventorySlotCommonStlye)) { // Debug.Log(chest.loot[cnt].ToolTip()); PlayerCharacter.Inventory.Add(chest.loot[cnt]); chest.loot.RemoveAt(cnt); } } //make sure we define where we are going to end the scroll view GUI.EndScrollView(); //set the tooltip to display if we have one. SetToolTip(); } /// <summary> /// Activate the loot window. /// </summary> private void DisplayLoot() { _displayLootWindow = true; } // private void PopulateChest(int x) { // for(int cnt = 0; cnt < x; cnt++) // _lootItems.Add(new Item()); // // _displayLootWindow = true; // } /// <summary> /// Close the loot window, and tell the lootable object that we have open that we are closing it. /// </summary> private void ClearWindow() { _displayLootWindow = false; //toggle the loot window display to off // _lootItems.Clear(); chest.OnMouseUp(); //let the lootable object that we currently have open to close chest = null; //clear the reference to a lootable object } /// <summary> /// Display the contents of the Inventory Window. /// </summary> /// <param name='id'> /// Identifier of the window that we are displaying the inventory elements in. /// </param> public void InventoryWindow(int id) { //create a counter to keep track of what item we are on. int cnt = 0; for(int y = 0; y < _inventoryRows; y++) { for(int x = 0; x < _inventoryCols; x++) { if(cnt < PlayerCharacter.Inventory.Count) { if(GUI.Button(new Rect(5 + (x * buttonWidth), 20 + (y * buttonHeight), buttonWidth, buttonHeight), new GUIContent(PlayerCharacter.Inventory[cnt].Icon, PlayerCharacter.Inventory[cnt].ToolTip()), inventorySlotCommonStlye)) { if(_doubleClickTimer != 0 && _selectedItem != null) { if(Time.time - _doubleClickTimer < DOUBLE_CLICK_TIMER_THRESHHOLD) { // Debug.Log("Double Click: " + PlayerCharacter.Inventory[cnt].Name); if(PlayerCharacter.EquipedWeapon == null) { PlayerCharacter.EquipedWeapon = PlayerCharacter.Inventory[cnt]; PlayerCharacter.Inventory.RemoveAt(cnt); } else { Item temp = PlayerCharacter.EquipedWeapon; PlayerCharacter.EquipedWeapon = PlayerCharacter.Inventory[cnt]; PlayerCharacter.Inventory[cnt] = temp; } _doubleClickTimer = 0; _selectedItem = null; } else { // Debug.Log("Reset the double click timer"); _doubleClickTimer = Time.time; } } else { _doubleClickTimer = Time.time; _selectedItem = PlayerCharacter.Inventory[cnt]; } } } else { GUI.Label(new Rect(5 + (x * buttonWidth), 20 + (y * buttonHeight), buttonWidth, buttonHeight), (x + y * _inventoryCols).ToString(), emptyInventorySlotStyle); } cnt++; } } SetToolTip(); // GUI.DragWindow(); } public void ToggleInventoryWindow() { _displayInventoryWindow = !_displayInventoryWindow; } public void CharacterWindow(int id) { _characterPanel = GUI.Toolbar(new Rect(5, 25, _characterWindowRect.width - 10, 30), _characterPanel, _characterPanelNames); switch(_characterPanel) { case 0: DisplayEquipment(); break; case 1: DisplayAttributes(); break; case 2: DisplaySkills(); break; } // GUI.DragWindow(); } public void ToggleCharacterWindow() { _displayCharacterWindow = !_displayCharacterWindow; } private void DisplayEquipment() { GUI.skin = mySkin; // Debug.Log("Displaying Equipment"); if(PlayerCharacter.EquipedWeapon == null) { GUI.Label(new Rect(5, 100, 40, 40), "lol", emptyInventorySlotStyle); } else { if(GUI.Button(new Rect(5, 100, 40, 40), new GUIContent(PlayerCharacter.EquipedWeapon.Icon, PlayerCharacter.EquipedWeapon.ToolTip()))) { PlayerCharacter.Inventory.Add(PlayerCharacter.EquipedWeapon); PlayerCharacter.EquipedWeapon = null; } } SetToolTip(); } private void DisplayAttributes() { // Debug.Log("Displaying Attributes"); } private void DisplaySkills() { // Debug.Log("Displaying Skills"); } private void SetToolTip() { if(Event.current.type == EventType.Repaint && GUI.tooltip != _toolTip) { if(_toolTip != "") _toolTip = ""; if(GUI.tooltip != "") _toolTip = GUI.tooltip; } } /// <summary> /// Display the tool tip if we have one. /// </summary> private void DisplayToolTip() { if(_toolTip != "") GUI.Box(new Rect(Screen.width / 2 - 100, 10, 200, 100), _toolTip); } }
I know they're pretty damn long, but I am working under a deadline and need urgent help. The BurgZergArcade forums did not give me any support at all, and so I came here, for the greater good. Please reply as soon as possible.
===================== ITEM GENERATOR SCRIPT =====================
using UnityEngine;
public static class ItemGenerator {
public const int BASE_MELEE_RANGE = 1;
public const int BASE_RANGED_RANGE = 5;
private const string MELEE_WEAPON_PATH = "Items/MELEE/";
public static Item CreateItem() {
//decide what type of item to make
//call the method to create that base item type
Item item = CreateWeapon();
// private string _name;
item.Value = Random.Range(1, 101);
item.Rarity = RarityTypes.Common;
item.MaxDurability = Random.Range(50, 61);
item.CurDurability = item.MaxDurability;
//return the new Item
return item;
}
private static Weapon CreateWeapon() {
//decide if we make a melee or ranged weapon
Weapon weapon = CreateMeleeWeapon();
//return the weapon created
return weapon;
}
private static Weapon CreateMeleeWeapon() {
Weapon meleeWeapon = new Weapon();
string[] weaponNames = new string[46];
weaponNames[0] = "7 branch sword";
weaponNames[1] = "aqua dagger";
weaponNames[2] = "aqua saber";
weaponNames[3] = "barbarian king axe";
weaponNames[4] = "blackblade dagger";
weaponNames[5] = "blood sword";
weaponNames[6] = "crystal hammer";
weaponNames[7] = "crystal sword";
weaponNames[8] = "darkness";
weaponNames[9] = "diamond hammer";
weaponNames[10] = "dragontooth spear";
weaponNames[11] = "emperor's oath";
weaponNames[12] = "fire claws";
weaponNames[13] = "fire knuckles";
weaponNames[14] = "fire scythe";
weaponNames[15] = "fireseed blade";
weaponNames[16] = "flametwirl blade";
weaponNames[17] = "frost knuckles";
weaponNames[18] = "frosty battering mace";
weaponNames[19] = "frosty skinner";
weaponNames[20] = "gladius";
weaponNames[21] = "golden claw";
weaponNames[22] = "great flametwirl blade";
weaponNames[23] = "great frosty sword";
weaponNames[24] = "great icefang";
weaponNames[25] = "great lightbane";
weaponNames[26] = "great shadow twister";
weaponNames[27] = "great shadowleak blade";
weaponNames[28] = "great emerald sword";
weaponNames[29] = "icefang";
weaponNames[30] = "lavarock blade";
weaponNames[31] = "lightbane";
weaponNames[32] = "lightning glove";
weaponNames[33] = "mithril knuckles";
weaponNames[34] = "plasma saber";
weaponNames[35] = "punishment";
weaponNames[36] = "rainbow sword";
weaponNames[37] = "shadow scythe";
weaponNames[38] = "shadowleak blade";
weaponNames[39] = "shadowtwister blade";
weaponNames[40] = "silver sword";
weaponNames[41] = "energy star saber";
weaponNames[42] = "star saber";
weaponNames[43] = "sun sword";
weaponNames[44] = "thief's knife";
weaponNames[45] = "tombstoner";
//fill in all of the values for that item type
meleeWeapon.Name = weaponNames[Random.Range(0, weaponNames.Length)];
//assign the max damage of the weapon
meleeWeapon.MaxDamage = Random.Range(5, 11);
meleeWeapon.DamageVariance = Random.Range(.2f, .76f);
meleeWeapon.TypeOfDamage = DamageType.Slash;
//assign the max range of this weapon
meleeWeapon.MaxRange = BASE_MELEE_RANGE;
//assign the icon for the weapon
meleeWeapon.Icon = Resources.Load(MELEE_WEAPON_PATH + meleeWeapon.Name) as Texture2D;
//return the melee weapon
return meleeWeapon;
}
}
public enum ItemType {
Armor,
Weapon,
Potion,
Scroll
}
Please take note that parts of these scripts have not yet been implemented.
"they are perfect"
first script has a switch on a string that could easily be a variable and cut the script down to half its size
facepalm
its part of a tutorial, masking everything behind a weapon class and simply going weapon.active = false might be great for efficiency but it is easier to $$anonymous$$ch people without completely going Object Oriented crazy.
This allows you to see and modify each weapon in a more obvious way.
if your intent is to not obfuscate everything behind a class it's a perfectly acceptable code.
Answer by sparkzbarca · Jan 20, 2013 at 07:07 PM
I can't believe i'm being so kind as to do some of this for you.
public string emptyInventorySlotStyle; Right now it does create an empty label, the problem is label is just a word or words and no word or words is basically nothing
you can change that line to
public string emptyInventorySlotStyle = "empty";
or any word or collection of words enclosed in quotations.
public string emptyInventorySlotStyle = "the words that I will see when there is no item";
Now have you used the loot stuff to loot anything? Created a loot chest and made it lootable and double clicked on it and grabbed the item from it?
Your inventory starts out empty, so of course you see nothing but empty labels.
In this code only by looting chests could you get items.
Yes, I have a fully functional randomly generating chest system with five items in each chest. The label is just a label, and is supposed to switch to a button with an icon over it upon double-clicking another item from the inventory. I know this is my fault, as I did not do a very good job at describing what I meant, and yeah, I feel a little stupid.
I have uploaded a video here, please check it, and maybe you can understand me better:
http://www.youtube.com/watch?feature=player_embedded&v=0ZPBr3WzNIU
Thanks in Advance
Need to see the debug log. this code has a lot of built in error checking but you have to check the log to see it
Press Control + shift + C or go to window -> console
AFTER YOU'VE PLAYED one run through.
It will throw up some errors (it's already thrown up one for example, the only one the video shows, its the little yellow triangle in the bottom left that says unable to find style in skin default, repaint.
It looks like the issue is probably either finding the weapon mount point or maybe with the weapon meshes but the contents of the console will tell us.
Yeah, only problem I had was that I included the wrong weapon mount location in the script, and so I fixed that. Now, however, that message does not show up anymore, but still, double-clicking in the inventory does not have any functionality at all. What is supposed to happen, is when I double-click an item, it will disappear from the inventory and reappear in the character window as the equipped weapon. I cannot achieve this functionality correctly.
Double-clicking does not do anything - I think the problem is in the GUI script.
do you have no errors now?
if double clicking is working there SHOULD be a line that says
double click: "insert name of clicked weapon here"
for example. Seeing the debug log would let me know if its getting that far.
Your answer
Follow this Question
Related Questions
GUI Button not working...but the the script is correct... 2 Answers
How to change button image on click 2 Answers
executing after i releasing button 2 Answers
Creating a GUI Button with more than one case 2 Answers
Gui Button Solid 2 Answers