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 /
avatar image
0
Question by Arethian · Jan 12, 2016 at 09:52 AM · collisionscripting beginnerscriptingbasicsfunctions

Can you trick a Ui Button that it is being clicked with code?

I've been wondering if a code (javascript or C#) could be set up that 'tricks' a ui button into acting as thought it were clicked. E.g When a the player (a rigid body, tagged player) enters a trigger collider, or a variable such as life becomes lower than 1, etc.

I'm not necessarily asking for a full on code, just if it can be done, and what function i'd have to use (and which code javascript/C#).

Cheers!

  • Arethian

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

3 Replies

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

Answer by JoshuaMcKenzie · Jan 14, 2016 at 05:39 AM

As people have posted, it can be done via the Button.onClick.Invoke() ... but as a personal rule of thumb (which stems from my experience in using MVC, or "Model-View-Controller" patterns), I'd advise that you shouldn't.

Doing so can potentially trap you into making confusing/conflicting logic, will increase the scope of a User-interface past what it should be responsible for, can make the UI rather inflexible, and your code won't be as loosely coupled as it could be (if you worry about that sort of thing)

GUI Elements and their attached viewscripts (as I tend to call them as they represent the "View" in MVC) shouldn't directly alter the state of the game. Your viewscripts should be allowed to:

  1. Read any data they need from the gamedata/playerdata (which represents the "Model" in MVC) and game controllers (representing the "Controller" in MVC).

  2. "Request" to change the state of the game (by calling methods on the controllers, which can choose to ignore the request if its invalid).

The major benefit to this rule is that now the dependency is one-way, your game state and Controller are not required to be aware of the existence of your viewscripts, which makes your controllers more robust, and your UI more modular, allowing you to swap out the UI for different UI on special occasions (for example perhaps you have a Hard Mode that disables or changes the in-game HUD).

Back to the question at hand.

This means that when a button is clicked, it will call a viewscripts OnClick() method (which you will write). and then this OnClick() will "ask" the controller to do the action (as well as update any UI if needed/successful). This means the actual game-changing logic is stored on the controller, so if you want to be able to "trick" a button to being clicked, you simply call the function on the controller that your OnClick() method would call. And the added benefit is that this button doesn't even need to exist in the first place to get the same functionality.

Comment
Add comment · 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
1

Answer by ashleyjlive · Jan 12, 2016 at 11:03 AM

If the desired result is to call the function for the onClick event it is as simple as getting the reference of the button and onClick.Invoke();

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 Arethian · Jan 14, 2016 at 03:47 AM 0
Share

Thank you! :D

(Not sure if I can/ How to make this an answer, since its a comment. Will Up-vote at least.)

avatar image
0

Answer by Riderfan · Jan 14, 2016 at 03:57 AM

Why do you need to invoke the onClick?

When you set up your GUI button, you're probably also giving it a function in a script to call when it's clicked. Just call that same function from other parts of your code, you don't need to call the 'click' function of the button, just to make the code in there execute.

Comment
Add comment · Show 2 · 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 ashleyjlive · Jan 14, 2016 at 04:20 AM 0
Share

I agree with this as it makes far more sense than tricking the UI. However, I was just answering his question to show him it is possible.

avatar image Arethian · Apr 07, 2016 at 11:18 PM 0
Share

I'm invoking the onClick so I don't have to use much other code*. I'm using the example button interface that lets you turn other objects on and off or change their variables.

*I've spent more time tinkering with the 'Shuriken' and legacy particles systems than java-script or C#.

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

6 People are following this question.

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

Related Questions

help to understand collision detection 1 Answer

how can I change a light with multiple triggers. ? 0 Answers

Turn Base Battle Simulation/Autobattle 0 Answers

Score not wokring the way I want it to 1 Answer

How do I improve my world generating script? 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