- Home /
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);
}
}
}
}
}
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.
I actually want it to be visible, but it isn't. But thanks for the hint with the grids!
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.
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
Follow this Question
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