how do i make an instantiated object be a child of DontDestroyOnLoad object's child ?
i tried it and final after searching a loot i couldn't find any answer to it .. i have a game object that contains a canvas, in that canvas i have inventory and i am dynamically adding items in to it so when ever i dynamically add new item it wont get parented to the slots and if i make the canvas not a part of DontDestroyOnLoad object then it works fine. i don't know what i am doing wrong but please help.. !
Correct me if I'm wrong.
If you do not use "DontDestroyOnLoad", you can add a gameobject as a child of any element in your canvas.
But, if you canvas is in DontDestroyOnLoad", you can't ?
Am I right ?
salanyel yes that's exactly what i am trying to do here. i want my canvas to be persistent scene to scene an my inventory is happen to be a part of it. so that what happen when i instantiate an item it gets instantiated but in the scene only not in the dontdestroyonload.
Answer by sheikh_Ati · Jul 28, 2017 at 12:55 PM
i think it was Unity bug ... ! i just remove the instantiate(item) and rewrite it, play it and it works.
If you rewrite your own code and that works, the problem is more in your own code ^^
Have a nice day !
Answer by Salanyel · Jul 28, 2017 at 10:38 AM
Normally, if your canvas is part of the Dontdestroyonload, it will be not destructible.
If you are not sure about how you can access your game object, I recommend to use GameObjet.FindObjectWithTag("MyCanvasTag"); and a specific tag. Like this, you can access your canvas from every scene because the canvas is still a part of the scene.
You have to parent your item to your canvas too. Be sure you really place your inventory in the canvas. Maybe creating a panel to access it easily ? Depending on what you want.
The last thing you can do is to call the function DontDestoryOnLoad(MyInventory"); when you have instanciated your object.
here is the image of the hierarchy and the slot is where i want it to get instantiated and be achild of it i am using generic list to stor the game object in it and using that list element i ma trying to make that instentiated object a part of slots.
itemObj.transform.SetParent(slots[i].transform); itemObj.transform.position = slots[i].transform.position + new Vector3(24, -25, 0);
Well, not a really generic method, but it seems it is working from the screenshot. Is it ?
nop ins$$anonymous$$d of instantiating i was trying to use a different approach but seems like its not working .. ! so i am back to where i was again. ! object gets instantiated but wont become a child of slot.
Your answer
Follow this Question
Related Questions
Can't Instantiate an object as child of another 1 Answer
My code is instantiating many prefabs, i only want one. 1 Answer
Generating prefabs at origin that are children of moving GameObjects? 2 Answers
How to spawn a prefab at a duplicate objects location 1 Answer
how do i instaniate prefab for once 0 Answers