- Home /
Having a plus button which adds objects
Hello ! I am a very new Unity developer. I am basically learning now and I was wondering how to make a simple button with "+" on it and when I tap it, to add a new game object (a simple cube) at a fixed (or random) location. If relevant, I am trying to build for iOS (Objective-C).
Thanks in advance ! Dan.
Answer by CHPedersen · Feb 18, 2014 at 02:34 PM
Fortunately, that's not so hard. All scripts have a callback called "OnGUI" into which you can define GUI elements. Unity's GUI system is immediate mode, which means there is no stored state, you define them with function calls that execute every single frame, and they return data that indicates how the user operated them that frame. The button is one such function, and it returns a boolean which is true during the frame the user clicked it. So, you handle whatever you want to happen inside an if-sentence for the button. A quick example that demonstrates it would look like this:
private void OnGUI()
{
if (GUI.Button(new Rect(50, 50, 25, 25), "+"))
{
GameObject newCube = GameObject.CreatePrimitive(PrimitiveType.Cube);
newCube.transform.position = new Vector3(0, 0, 0);
}
}
Put that in a script and it'll do what you asked, with a fixed location at the coordinate system origin. If you want randomized positions, look into the Random class. If you want an image on the button instead of a simple "+"-character, that's possible too, study the documention for GUI.Button. Good luck experimenting!
Please don't $$anonymous$$d me for asking, but can you please be more explicit on how to do this exactly ? Sorry again for my newbie questions, but there's a day or two since I'm using Unity. I would really appreciate it. If not, I still thank you for your kind answer. I hope I will eventually find the way to implement your solutions.
Best of luck ! Dan.
I'd be happy to, so... err, taking this from the beginning? For starters, there is no Objective-C here. Unity uses only JavaScript (UnityScript), Boo or C#. What I posted was C#. To see it in effect, makea new GameObject in the editor: GameObject-> Create Empty. Then make a new C# Script in the editor: Create New-> C# Script. It will be created with a stub containing a Start and an Update method. Leave those alone. Copy paste the code I posted into the new C# script and save it. It must be alongside the existing Start and Update, not inside any of them. Now drag this script onto the empty GameObject you created previously, so it is attached to it. Click play in the editor. The +-button appears.
Don't know if I'm going too far, but how can I add gravity to the newly added button or any other customizations ? (like color and so on).
I don't understand what "gravity" is in this context. Other visual properties are customizable using a GUIStyle object which you set up with the properties you want prior to calling GUI.Button.
This is a separate question, however, so I invite you to open a new Question about GUIStyles and GUI.Button. :)
Answer by Avi Dwivedi · Feb 19, 2014 at 10:24 AM
Hi praleadanut
i m also an iOS developer and recently started coading for unity iOS
what i found new about unity that its extremely powerful game engine, and sometime situations came where you iOS devices are not even able to Calculate such complexity. So keep in mind to optimise things from the starting of making a project good luck.
thanks for the advice ! you can have my email if you find yourself creating something interesting :)