c# - Instantiate method doesn't draw the prefab button inside the canvas and doesn't draw it with correct prefab dimensions -
my scene has basic game objects (camera, canvas 2 child image
, button
).
i make prefab of button, prefab on project view, want instantiate prefab button within script, , want drawn inside canvas.
for that, make script file, attach canvas script component. here script implementation:
using unityengine; public class quizmanager : monobehaviour { public transform suggestionbtn; // use initialization void start () { instantiate (suggestionbtn, new vector3 (100, 400, 0), quaternion.identity); } // update called once per frame void update () { } }
of course, suggestionbtn prefab, that's why make reference of script variable (drag prefab project view script component).
now when run game, noticed clone of prefab added above game objects in hierarchy view (i expecting added inside canvas):
and has wrong dimension (very small, barely visible), here how looks after zoom in
so question how can instantiate prefab correctly normal size , position correctly relatively canvas (child of canvas) ?
thanks
you can correctly initialize transform (under correct game object hierarchy) assigning instance variable , changing parent.
public class quizmanager : monobehaviour { public transform suggestionbtn; // use initialization void start () { transform clone = (transform)instantiate (suggestionbtn, new vector3 (100, 400, 0), quaternion.identity); // make instance child of current object clone.parent = gameobject.transform; // adjust scale clone.transform.localscale = new vector3(xval, yval, zval); } }
you can change localscale
assigned clone
variable after instantiation.
Comments
Post a Comment