Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 C00kkie · Jul 07, 2020 at 09:20 PM · uicanvasinventory

How do I make an instantiated UI GameObject visible without parenting it to the Canvas?

I have an Inventory that I want to set up. That Inventory has a set number of slots. If all slots are full, I want to instantiate another slot next to the last slot. That somehow never is visible, except for when I set it as a parent of the Canvas. In that case it is instantiated in a weird place with a weird scale.

But the actual issue I have is that I need to instantiate it with the parent kette (that is the GameObject where all the slots are attached to). I have to buttons with which I can move all slots by moving the GameObject kette. In order for the instantiated slot to move with the other slots I need to set kette as a parent. But if I do that the object is not visible.

How can I assign the Canvas and kette to the new slot? Or perhaps there is another way to solving this issue?

{

 public List<bool> isFull = new List<bool>();
 public List<GameObject> slots = new List <GameObject>();
 public GameObject slot;
 public GameObject kette;

 Vector2 newSlotPosition;
 Vector2 newKettePosition;

 void Update()
 {
     if (Input.GetMouseButtonDown(0))
     {
         Vector3 mousePos = Camera.main.ScreenToWorldPoint(Input.mousePosition);
         Vector2 mousePos2D = new Vector2(mousePos.x, mousePos.y);

         RaycastHit2D hit = Physics2D.Raycast(mousePos2D, Vector2.zero);

         if (hit.collider != null)
         {
             if (hit.collider.tag == "InventoryObject")
             {
                 // hit.transform.position = mousePos2D;
                 if (isFull.Contains(false))
                 {
                     for (int i = 0; i < slots.Count; i++)
                     {
                         if (isFull[i] == false)
                         {
                             // for the next slot that is empty add the InventoryGameObject to the slot
                         }
                     }
                 }
                 else  // if all slots are full
                 {
                     Debug.Log("full");
                     newSlotPosition = new Vector2(slots[slots.Count - 1].transform.position.x + 100f, slots[slots.Count - 1].transform.position.y);
                     GameObject newSlot = Instantiate(slot, newSlotPosition, transform.rotation);
                     newSlot.transform.SetParent(kette.transform, true);
                     newSlot.transform.SetParent(GameObject.FindGameObjectWithTag("Canvas").transform, true);
                     
                     slots.Add(newSlot);
                     //hit.collider.transform.position = slots[slots.Count - 1].transform.position;
                     InventoryObject insideInventoryObject = hit.collider.gameObject.GetComponent<InventoryObject>();
                     GameObject newInsideInventoryObject = Instantiate(insideInventoryObject.miniInventoryObject, newSlotPosition, transform.rotation);
                     newInsideInventoryObject.transform.SetParent(slots[slots.Count - 1].transform);
                     // newInsideInventoryObject.transform.parent = slots[slots.Count - 1].transform;
                     isFull.Add(true);
                 }
             }
         }
     }

 }



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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ray2yar · Jul 08, 2020 at 02:20 AM

So you want the new "slot" that is past the "full" to be invisible? Why not set its active value to false? Also, for weird formatting or placement have you tried using panels and layout groups / grids? Sometimes it helps to set the localscale to vector3.one after instantiating.

Comment
Add comment · Show 3 · 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 C00kkie · Jul 08, 2020 at 06:05 AM 0
Share

I actually want it to be visible, but it isn't. But thanks for the hint with the grids!

avatar image ray2yar C00kkie · Jul 08, 2020 at 11:42 AM 0
Share

Got you! Easy - on your UI element prefab have it in it's own canvas. To show UI elements need to be on a canvas. This solution will present a couple other problems, but, that's what you need to do.

avatar image C00kkie ray2yar · Jul 09, 2020 at 07:55 PM 0
Share

Thank you for the answer. Didn't add a new canvas though. Your first comment re$$anonymous$$ded me that I could just put all slots there and hide the ones I don't need ^^'

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

230 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 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

New UI Is active but not drawing 0 Answers

Why do contents of nested Panel spill outside parent? 0 Answers

Close UI using Canvas Group 1 Answer

Problem with Fonts rendering 1 Answer

4.6 UI Canvas is smaller than normal 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