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). enter image description here

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).

enter image description here

now when run game, noticed clone of prefab added above game objects in hierarchy view (i expecting added inside canvas):

enter image description here

and has wrong dimension (very small, barely visible), here how looks after zoom in

enter image description here

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

Popular posts from this blog

java - Plugin org.apache.maven.plugins:maven-install-plugin:2.4 or one of its dependencies could not be resolved -

Round ImageView Android -

How can I utilize Yahoo Weather API in android -