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 Geiun · Aug 30, 2014 at 06:47 PM · c#gui

How to make a button script.

I'm using Unity 4.6, and and very new to Unity.

So I created a UI in the editor, and now I want to add some functionality to the buttons in that UI. I've created a C# script and attached it to the relevant button object, but I'm not sure how to call a specific C# function when the button is clicked.

I've done some googling, and decided that OnGUI was the way to go... But using OnGUI triggers even if I click nowhere near the attached button.

I've done some more googling, and decided that what I want is to implement OnMouseUpAsButton, but now nothing seems to happen when I click the button.

Here is my code, if you're interested. using UnityEngine; using System.Collections;

 public class btnPlay : MonoBehaviour {
 
     private float startTime = 0.0f;
     private float animDuration = 0.5f;
     private bool animated = false;
     private float startAngle = 0.0f;
     private float endAngle = Mathf.PI / 4;
     private float Z = 0;
     private float M = 0;
 
     // Use this for initialization
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         if (animated) {
             float currentTime = Time.realtimeSinceStartup;
             if (currentTime - startTime > animDuration) {
                 Camera.main.transform.position = new Vector3(M * Mathf.Cos(endAngle), M * Mathf.Sin(endAngle), Z);
                 animated = false;
             }
             // Transform
             float dt = (currentTime - startTime) / animDuration;
             float theta = (endAngle - startAngle) * dt + startAngle;
             Camera.main.transform.position = new Vector3(M * Mathf.Cos(theta), M * Mathf.Sin(theta), Z);
         }
     }
 
     void OnMouseUpAsButton () {
         startAngle = Mathf.Atan2(Camera.main.transform.position.y, Camera.main.transform.position.x);
         Z = Camera.main.transform.position.z;
         M = Mathf.Sqrt(Mathf.Pow(Camera.main.transform.position.x, 2) + Mathf.Pow(Camera.main.transform.position.y, 2));
         startTime = Time.realtimeSinceStartup;
         animated = true;
     }
 }

Any help would be very appreciated.

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

Answer by AyAMrau · Aug 30, 2014 at 06:59 PM

If you are using the new UI, the Event System handles catching the events (such as clicking).

All you need to do is add a functionality to the button.

This thread should help you to at least get it set up

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 Geiun · Aug 31, 2014 at 06:30 AM 0
Share

Can you provide a functional or $$anonymous$$imal example of it in action? I've tried the stuff in that link, but that code won't compile. It complains about the type 'Button' not being found.

avatar image Kiwasi · Aug 31, 2014 at 07:04 AM 0
Share

Couple of reasons why it might not find the type.

  1. You need to include the UI in your using or import statements at the top of your script

  2. Your not using the beta version of Unity 4.6

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

What is frame, How OnGuI is called every frame? 2 Answers

What Am I Doing Wrong? Variable Names 3 Answers

Inputing an Equation 0 Answers

How to dynamically change the text in Unity(Augmented Reality + NYARtoolkit(C#)) ? 0 Answers

The name 'Joystick' does not denote a valid type ('not found') 2 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