Question by
Lazerturkey · Jun 26, 2017 at 11:59 AM ·
gameobjectprefabbutton
How to creating a Gameobject from a prefab via scripted UI BUTTON
Hello Community,
I have a problem to create a Gameobject with an scripted UI Button. My situation:
I created a Button with a script:
void OnGUI() {
GUI.Button (new Rect (800, 5, 115, 30), "Save Coordinate");
}
Now I want to created a Gameobject from a prefab (in my case a blue sphere) by clicking on the Button. The prefab of the Blue sphere is called "CoordPoint". The position of the Gameobject doesn't matter for now. I am very new to programming and couldnt handle to put a working script together from all the documentations about Buttons and creating Gameobjects.
I made it work with a UI Button created in Unity, but for me now, it is important to use a scripted button. If more inputs for my problem are needed, please let me now.
Kind regards
Comment
Answer by vir1234 · Jun 30, 2017 at 01:30 PM
try this If i am not wrong
public GameObject CoordPoint;
void OnGUI()
{
if (GUI.Button (new Rect (800, 5, 115, 30), "Save Coordinate"))
{
Instantiate (CoordPoint, new Vector3 (0, 0, 0), Quaternion.identity);
}
}