Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

How To Show Prefab Object on Plane When Button GUI clicked in unity c#

Discussion in 'Scripting' started by sandroe, Sep 4, 2014.

  1. sandroe

    sandroe

    Joined:
    Aug 30, 2014
    Posts:
    10
    Hi All,
    I'm newbie in unity and c#..please help me to show prefab object when i clicked a gui button..
    For example:
    when i click button "ANG", so the prefab ANG show in plane.
    This is my script my GUI Button.

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class GUI_left : MonoBehaviour {
    5.  
    6.     public Rect windowRect = new Rect(20, 20, 120, 50);
    7.  
    8.     void OnGUI() {
    9.         windowRect = GUILayout.Window(0, windowRect, DoMyWindow, "Membuat Skenario", GUILayout.Width(200));
    10.     }
    11.  
    12.     void DoMyWindow(int windowID) {
    13.         if (GUILayout.Button("ANG")){
    14.             print("Anda Memilih ANG");
    15.         }
    16.         if (GUILayout.Button("ARHANUD")){
    17.             print("Anda Memilih ARHANUD");
    18.         }
    19.         if (GUILayout.Button("ARMED")){
    20.             print("Anda Memilih ARMED");
    21.         }
    22.         if (GUILayout.Button("HUB")){
    23.             print("Anda Memilih HUB");
    24.         }
    25.         if (GUILayout.Button("INF")){
    26.             print("Anda Memilih INF");
    27.         }
    28.         if (GUILayout.Button("KAV")){
    29.             print("Anda Memilih KAV");
    30.         }
    31.         if (GUILayout.Button("KES")){
    32.             print("Anda Memilih KES");
    33.         }
    34.         if (GUILayout.Button("PAL")){
    35.             print("Anda Memilih PAL");
    36.         }
    37.         if (GUILayout.Button("PNB")){
    38.             print("Anda Memilih PNB");
    39.         }
    40.         if (GUILayout.Button("POM")){
    41.             print("Anda Memilih POM");
    42.         }
    43.         if (GUILayout.Button("ZI")){
    44.             print("Anda Memilih ZI");
    45.         }
    46.    
    47.     }
    48. }
    question button 1.png question button.png

    Please Help me to show prefab in plane when i click the button...Thank's before it...
     
  2. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    Syntax

    Code (CSharp):
    1. static Object Instantiate(Object original, Vector3 position, Quaternion rotation);
    Example
    Code (CSharp):
    1. if (GUILayout.Button("ANG")){
    2.             print("Anda Memilih ANG");
    3.           Instantiate(ANG, new Vector3(i * 2.0F, 0, 0), Quaternion.identity) as Transform;
    4.         }
     
  3. sandroe

    sandroe

    Joined:
    Aug 30, 2014
    Posts:
    10

    Hi parandham03,
    I still get error when i add this script to my project.. This is the screenshot :
    question button cont.png
     
  4. parandham03

    parandham03

    Joined:
    May 2, 2012
    Posts:
    174
    can u post code