• 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 /
  • Help Room /
avatar image
0
Question by haam · Jun 09, 2016 at 08:24 AM · c#unity 5button

ui button function and onclick event

how can i give this ui button an onclick event as well as a keyboard return key ,i think i am not being clear enough ,i want my game to have the same function when i click return and when i click the button on screen ,i for keyboard return one for screen button ,basically how can i give 2 key for same function` public Transform eggpoint;

%|-1560315035_1|%

 void Start()
 {

 }


 // Update is called once per frame
 void Update()
 {
     if (Input.GetKeyUp("space") || Input.GetMouseButtonDown(0))
     {
         GetComponent<Rigidbody2D>().velocity = Vector2.zero;
         GetComponent<Rigidbody2D>().AddForce(jumpForce);
     }
         if(Input.GetKeyDown(KeyCode.Return))
         {
             Instantiate(Egg, eggpoint.position, eggpoint.rotation);

%|657980421_16|%

%|69678579_17|% %|1246395602_18|% }`

as you can see i have instantiate function above and now i have the return key from keyboard to execute this fucntion i want my ui button to have the same function how can i achieve this.

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

1 Reply

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

Answer by Mmmpies · Jun 09, 2016 at 08:51 AM

Just create a public function that does the same...

 public void LayEgg()
 {
      Instantiate(Egg, eggpoint.position, eggpoint.rotation);
 }

Then on the button click + in the OnClick, drag the object that your script is on to the slot that appears and select YourScriptName -> LayEgg from the drop down.

It makes sense to call that function from you key input as well so any changes you make only need to be made i one location.

Comment
Add comment · Show 3 · 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 haam · Jun 09, 2016 at 10:21 AM 0
Share

but the problem is as you can i have to function over there getmousebuttondown which make my player jump and now when i press the lay egg button its laying an egg and jumping togather how can i fix this

avatar image Mmmpies haam · Jun 09, 2016 at 10:49 AM 0
Share

$$anonymous$$ixing Get$$anonymous$$ousebuttonDown and UI input with the mouse is going to cause issues. You'd be better of creating a public void Jump() function and have a button take up the rest of the screen in the UI that calls that function via OnClick as above.

You don't have to have an image or text on your button so it can be completely transparent. Or you can put a CanvasGroup on it and set the Alpha to zero (again it makes it invisible).

Then if your space key is pressed or that OnClick on the big invisible button happens call Jump() and that adds your force.

avatar image haam · Jun 09, 2016 at 10:55 AM 0
Share

0 Answer by haam · 1 sec ago thank you soo much ,you have been very help full

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

183 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image

Related Questions

Creating dynamically moving buttons/GUI objects. 1 Answer

Next and Back Button? 0 Answers

Need help with Mute Button 1 Answer

Change color for disabled button 1 Answer

Someone please help me with combination lock 0 Answers

  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges