Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
avatar image
0
Question by GreySkyGames · Feb 21, 2019 at 07:10 AM · gameobjectinstantiatebutton

Buttons not showing up the first time the menu loads, but showing up on all menu openings after

I'm trying to program an inventory menu in my game, but for some reason the buttons are not showing up the first time I open it. They are showing up in the hierarchy, but they are not visible. If I close the menu and open it again, they'll be visible every time after the first.

I've checked the invisible buttons in the inspector and everything about them seems identical to the buttons that show up on the second menu opening. Z axis, layer, Image (Script), Button(Script)...

Here's the code I'm using to place the buttons:

     public void InitializeInventoryMenu()
     {
         //Activate inv canvas
         InventoryCanvas.gameObject.SetActive(true);
 
         //Clear existing buttons
         UI_Inventory_InvButton[] existingButtons = GetComponentsInChildren<UI_Inventory_InvButton>();
         foreach (UI_Inventory_InvButton button in existingButtons)
         {
             button.gameObject.SetActive(false);
         }
 
         //Set up buttons
         foreach (KeyValuePair<string, StaticData.WoolType> entry in StaticData.staticData.woolTypesDict)
         {
             if (GameControl.control.woolInventory[entry.Key] > 0)
             {
                 Debug.Log("Adding new entry");
                 GameObject myObject = Instantiate(Button_Template);
                 UI_Inventory_InvButton storeButton = myObject.GetComponent<UI_Inventory_InvButton>();
                 storeButton.SetupButton(entry.Key);
                 myObject.transform.SetParent(Button_Template.transform.parent);
                 myObject.SetActive(true);
             }
         }
     }


I have tried changing the order of some of the steps but that hasn't changed the result.

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by GreySkyGames · Feb 23, 2019 at 11:01 AM

Moved on to other phases in my project, fiddled with art assets for a bit and came back to it...Turns out I missed something when looking at the values of the "invisible" vs visible buttons. The X, Y, and Z scale were all set to 0 on the invisible buttons. Adding "myObject.gameObject.transform.localScale += new Vector3(1, 1, 1);" fixed the issue.

I really don't understand why the scales got set to zero or why they only got set to zero the first time...if anyone has any insight on that I'm super curious.

Comment
Add comment · Share
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
avatar image
0

Answer by SteenPetersen · Feb 21, 2019 at 08:08 AM

Hi there

First it seems a bit odd that you are running a loop to remove some already existing buttons from the scene when you load the inventory, would it not be easier to have those buttons in a canvas that you can simply disable? I suspect that:

 foreach (UI_Inventory_InvButton button in existingButtons)
 {
  button.gameObject.SetActive(false);
 }


Is where your problem is. Perhaps the first time it runs its hides those buttons, and in subsequent iterations it works correctly. When you press 'play' and they dissapear, are they disabled in the hierarchy?

Comment
Add comment · Show 1 · Share
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
avatar image GreySkyGames · Feb 22, 2019 at 12:40 AM 0
Share

$$anonymous$$y thought was that the items the player has in their inventory would change so I'd re-render it on each open. But it probably would be better to just detect which items need to be added and removed rather than redraw the whole thing.

Removing that loop doesn't change the result. At least not for the first time the inventory opens. The first time the inventory is opened, set of invisible buttons are there. The second time it opens, the visible buttons are added to the grid layout after the invisible buttons.

It's not so much me hitting play and the buttons disappearing so much as me hitting play, opening the inventory, and getting a blank inventory. The invisible buttons that are added do show up in the hierarchy, and the appropriate amount of "Adding new entry" debug logs (line 18 in the code I posted) fire off.

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

176 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Bullet Prefab not instantiating 1 Answer

[SOLVED] Only instantiating once 1 Answer

Instantiate object with button and place another object with mouse click 2 Answers

Button then instanitates gameobject 1 Answer

Bearded Multi-Headed Instantiation Dragoon Question 1 Answer


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