I would like to know how to use a button click to bring objects to the scene.
-
I would start by looking here. Also, please read How to Ask.Nahuel Ianni– Nahuel Ianni2016-06-27 14:12:13 +00:00Commented Jun 27, 2016 at 14:12
-
Once again, please read 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– Nahuel Ianni2016-06-27 14:30:13 +00:00Commented Jun 27, 2016 at 14:30
2 Answers
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.
2 Comments
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);
}
}