Yetter
1
I was fighting with this problem all last night and couldn’t figure out the solution. Im making a turnbased RPG, when the abilities button is pressed, a menu should spawn with a button for every ability unlocked.
so I’ve declared both the prefab button and the canvas I want to use as a parent for the button, then I wrote a function to be called whenever the ability menu button is pressed that should instantiate the button and set the parent.
`
public Button actionButton;
public Canvas actionMenu;
pubilc void ActionMenuActivate(int abilitiesAvailable){
Instantiate(actionButton);
transform.actionButton.SetParent(transform.actionMenu,false);
`
but the button always shows up far away from my canvas and scene. When I just drop it into the hierarchy on to the actionMenu it works as expected. Any help would be greatly appreciated.
PS any advice on formatting my code on these questions would also be appreciated.
vintar
2
This should work , but create a prefab of your action button :
public GameObject actionButton;
public Canvas actionMenu;
pubilc void ActionMenuActivate(int abilitiesAvailable)
{
GameObject obj = Instantiate(actionButton);
obj.transform.SetParent(actionMenu.transform,false);
}
Yetter
3
Well I can’t explain it, still isn’t working. was almost sure that assigning the instance was gonna fix this problem but I’m still having the exact same issue, the button keeps its RectTransform but it is releative to the world instead of the canvas. Appreciate the input though, Thanks
EDIT: well this is interesting, in the console I have this warning
" Setting the parent of a transform which resides in a prefab is disabled to prevent data corruption.
UnityEngine.Transfor:SetParent(Transform,Boolean)"
just to be clear actionMenu is also stored as a prefab