Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by praleadanut · Feb 18, 2014 at 02:27 PM · gameobjectiosgenerationobjective-c

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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!

Comment
Add comment · Show 5 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image praleadanut · Feb 18, 2014 at 08:32 PM 0
Share

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.

avatar image CHPedersen · Feb 18, 2014 at 09:04 PM 1
Share

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.

avatar image praleadanut · Feb 19, 2014 at 10:10 AM 0
Share

you are the best !

avatar image praleadanut · Feb 19, 2014 at 12:55 PM 0
Share

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).

avatar image CHPedersen · Feb 19, 2014 at 02:28 PM 0
Share

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. :)

avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image praleadanut · Feb 19, 2014 at 12:34 PM 0
Share

thanks for the advice ! you can have my email if you find yourself creating something interesting :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

20 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Adding gravity and texture to newly added game objects 1 Answer

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Camera to follow a target within a circle? 1 Answer

Does iOS UnitySendMessage queue up whilst loading scenes? 0 Answers

Combine Cocos2d-X mini games on top of Unity game 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges