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
Share
Go down
Member [Lv. 0]
Member [Lv. 0]
Posts : 1
Points : 201
Rep : 0
Join date : 2018-01-03
View user profile

How to Instantiate buttons correctly?

on Wed Jan 03, 2018 1:12 pm
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?
Member [Lv. 1]
Member [Lv. 1]
Posts : 3
Points : 817
Rep : 0
Join date : 2016-05-17
View user profile

Re: How to Instantiate buttons correctly?

on Thu Jan 11, 2018 7:33 pm
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.
Member [Lv. 1]
Member [Lv. 1]
Posts : 3
Points : 817
Rep : 0
Join date : 2016-05-17
View user profile

Re: How to Instantiate buttons correctly?

on Fri Jan 12, 2018 3:49 am
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
Back to top
Permissions in this forum:
You cannot reply to topics in this forum