1

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

2
  • I would start by looking here. Also, please read How to Ask. Commented 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. Commented Jun 27, 2016 at 14:30

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thx it was helpful can you just tell me more because I do not understand 4th step. Please. Thank you
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 :)
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); 
                }
            } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.