1

I would like to know how to use a button click to bring objects to the scene.

sena
  • 13
  • 1
  • 5
  • I would start by looking [here](http://docs.unity3d.com/Manual/CreateDestroyObjects.html). Also, please read [How to Ask](http://stackoverflow.com/help/how-to-ask). – Nahuel Ianni Jun 27 '16 at 14:12
  • Once again, please read [How to Ask](http://stackoverflow.com/help/how-to-ask). If you have a new question, post it as such. Stating "I have a problem" without explaining it and detailing ALL the relevant code will make it impossible for us to help. – Nahuel Ianni Jun 27 '16 at 14:30

2 Answers2

4

1) Create a button using Unity GUI system.

2) Create a script:

public GameObject sampleObject;

public void AddObject()
{
    Instantiate(sampleObject, Vector3.zero, Quaternion.Identity);
}

3) Attach this script to an object in the scene, and set a prefab to sampleObject.

4) Select your button and in the Inspector add a new OnClick script, and select the object with the new script attached, select AddObject() method.

Now when you click on the button it should instantiate an object at (0.0f, 0.0f, 0.0f).

Hope that helps you.

  • Thx it was helpful can you just tell me more because I do not understand 4th step. Please. Thank you – sena Jun 30 '16 at 15:11
  • 1
    Well, when you select your button, you should see at the bottom of the inspector window a little + button, this will add a new OnClick behavior, in there you should have a script variable, just drag and drop the object with the script attached. The you should be able to select a public method from that script using the drop down menu. Hope that clarified the step :) – Primaël Quémerais Jun 30 '16 at 15:34
0

I think use gameObject z postion value and show or hide when this object allready created

Find current gameObject and set transform.postion.z = -1 or 1

if gameObject z postion set to -1 hideObject else showObject

sampleCode

 float yourChose = -1f; // chose object hide or show (-1 or 1 )

 foreach (var item in  FindObjectsOfType(typeof(GameObject)) as GameObject[])
            {                               
                if (item != null && item.name == "CurrentObjectName")
                {
                    item.transform.position = new Vector3(item.transform.position.x, item.transform.position.y, yourChose); 
                }
            }