UnityNinja Community
Would you like to react to this message? Create an account in a few clicks or log in to continue.

UnityNinja CommunityLog in

UnityNinja - Video Game Development Community, including Resources, Forums, Marketplace & More.


descriptionHow to Instantiate buttons correctly? EmptyHow 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?

descriptionHow to Instantiate buttons correctly? EmptyRe: 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.

descriptionHow to Instantiate buttons correctly? EmptyRe: 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

descriptionHow to Instantiate buttons correctly? EmptyRe: How to Instantiate buttons correctly?

more_horiz
privacy_tip Permissions in this forum:
You cannot reply to topics in this forum
power_settings_newLogin to reply