Unity Ninja Unity 3D Forum
To have exclusive access to all downloads, recourses and links within this forum, a free registered account is required. Just a heads up Wink

You are not connected. Please login or register

Unity Ninja Unity 3D Forum » HELP » Help (General) » How to Instantiate buttons correctly?

How to Instantiate buttons correctly?

View previous topic View next topic Go down  Message [Page 1 of 1]

1 How to Instantiate buttons correctly? on Wed Jan 03, 2018 1:12 pm

cremefresh


Member [Lv. 0]
Member [Lv. 0]
I really have a problem instantiating a Button.
I did the following: I created a button in canvas and added it to prefaps. After that I deleted the button in the Hierachy. I made a Script:


public class ButtonPrefab : MonoBehaviour {

public GameObject buttonPrefab;
GameObject buttonPrefabClone;

void Start() {
buttonPrefabClone = Instantiate(buttonPrefabClone,transform.position,Quaternion.identity) as GameObject;

} }

Finally I created an Empty Object, put the script inside it and added the prefab button.
What I get is a clone button which is WAY out of the canvas and WAY too large. On top of that, I cannot see the Image of the button. I did the exact same thing with a cube prefab and it works perfectly (the cube spawns right there were I put the prefab cube in).

Why doesnt it work? What am I missing?

View user profile

2 Re: How to Instantiate buttons correctly? on Thu Jan 11, 2018 7:33 pm

nicox17


Member [Lv. 1]
Member [Lv. 1]
Hello ! so you want to create a prefab at the same position of an empty Gameobject when starting game?
Try this:

public class ButtonPrefab : MonoBehaviour {

public GameObject buttonPrefab;


void Start() {
Instantiate(buttonPrefab,gameobject.transform.position,gameobject.transform.rotation) ;

} }

Put the script on your empty gameobject and paste your prefab in the "buttonPrefab" field.

View user profile

3 Re: How to Instantiate buttons correctly? on Fri Jan 12, 2018 3:49 am

nicox17


Member [Lv. 1]
Member [Lv. 1]
public class ButtonPrefab : MonoBehaviour {

public GameObject buttonPrefab;
public bool iscreated=false;


void Start() {
if(iscreated=false){

Instantiate(buttonPrefab,gameobject.transform.position,gameobject.transform.rotation) ;
iscreated=true

} } }


Works better and only one prefab will be instantiate

View user profile

Sponsored content


View previous topic View next topic Back to top  Message [Page 1 of 1]

Permissions in this forum:
You cannot reply to topics in this forum