Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • 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.

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

How to use one button to play different audio for different Targets? 0 Answers

Add an Icon next to a text inside a button Unity 5.6 1 Answer

Can't access functions on my UI button 1 Answer

button function on unity5 1 Answer

Creating dynamically moving buttons/GUI objects. 1 Answer


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