- Home /
Is it possible to link a UI element to a gameobject?
I have three images displayed in my game and my player needs to choose between them (it's a character-selection type of thing). I've built UI buttons for each and they are positioned correctly to be used when testing in the editor, but because they are parented to the Canvas, they shift position and are out of place when I build the game.
I've tried moving the button to parent it to the same empty game object as the sprite in the scene itself, but then the button just disappears and can't be seen at all in either the editor or the game (although it is in the hierarchy still).
Is there a way to force the link to the gameobject/sprites instead? Or is my only option to build a button without the built-in UI system, just with coding?
Okay, so I've gone through all the links that have been given to me but I'm still struggling. When I attempt to instantiate it, the console tells me I cannot set the parent of a prefab object to prevent data corruption and the 'button' is still invisible. If I drag it directly into the canvas in the hierarchy, then it will reappear immediately.
This is my code, where 'redwizard button' is the button I'm trying to keep near the 'redwizard' sprite: (Also note the final position hasn't been decided, I'm just trying to instantiate it at all, which is why the button would currently be appearing on top of the sprite image)
public Button redwizardbutton;
public GameObject redwizard;
void CreateWizard () {
Vector3 redwizardspawnposition = new Vector3 (
redwizard.transform.position.x,
redwizard.transform.position.y,
0);
Instantiate (redwizardbutton, redwizardspawnposition, Quaternion.identity);
redwizardbutton.transform.SetParent (redwizard.transform);
}
Here's the screenshot with the instantiated object selected (the button should say 'Choose' and appear underneath the red wizard).
Here's a screenshot of where the button actually shows up.
The Quick solution is I think you have to make some a canvas for each like set of the canvas which will instantiate same time when your character instantiated.
Answer by rajavamsidhar_gvs · Aug 10, 2015 at 11:48 AM
hi...i faced this problem.better to instantiate that UI object if you must want built in UI. otherwise just go with default unity scripting.and once take help from google.
Instantiate(canvas,new vector3 (0,0,0),quaternion.Identity);
Thanks! And I guess you'd have to instantiate as a child object as well.
yes..you can use " gameobjectname.transform.parent ".
I tried instantiating the UI, but it didn't show up. I think it's because it can't be the child of both the Canvas and the object I want to offset from. How did you manage to get it to be both?
sorry for late.and when i'm changing canvas positions or adding it as child to any other object its not work.its better to just use instantiate. and dont go for parent.it will work surly. and can you tell why do you want it as child for a empty gameobject?
Even when I instantiate it, it doesn't show up on screen (although it does in the hierarchy). It has to be a child of the Canvas in order to show up, but I want it to be linked to another 'normal' sprite (or else it will end up being off-position when the player chooses different screen resolutions). How did you get it to actually appear on the screen when instantiated?
Answer by moriggi · Aug 15, 2015 at 10:07 PM
hi create a canvas, inside it put 3 images of the 3 wizards and position all in the right position and pivot by the RectTransform component. then put as child of canvas the 3 buttons. add an event of the button, drag and drop the Game Object containing the function you need to execute inside the button event and select public void CreateWizard(). when the button is clicked the function is executed. do it for all the 3 buttons