FragMate CommunityLog in

FragMate - Software and Game Developer Community, including Resources, Forums, Marketplace & More.

Share

descriptionHow to Instantiate buttons correctly?

more_horiz
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?

descriptionRe: How to Instantiate buttons correctly?

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

descriptionRe: How to Instantiate buttons correctly?

more_horiz
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
Permissions in this forum:
You cannot reply to topics in this forum