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 Klaster55 · Oct 07, 2015 at 03:36 AM · button trigger events

Script being autorun on start instead of button click.

I am trying to have a script run when a button is clicked, but it just automatically runs when play is pressed in the editor. I have the script attached to an empty game object and the added onto the Button (Script) component under "On Click ()." This is the script:

 using UnityEngine;
 using System.Collections;
 
 public class ButtonPress : MonoBehaviour {
     
     int number;
     
     void Start() {
         number = Random.Range(1,1000);
     }
     
     void OnGUI() {
         Rect oRectangle = new Rect(10, 10, 200, 200);
         GUI.Label(oRectangle,"The number that was generated: " + number);
     }
 }

Any help would be greatly appreciated (I am a serious noob)!

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
0

Answer by MadDevil · Oct 07, 2015 at 04:27 AM

@Klaster55

Start, will be called every time you press the play in editor.

http://docs.unity3d.com/ScriptReference/MonoBehaviour.Start.html

so instead call a function on the button press.

void RandomGenerator() { number = Random.Range(1, 1000); }

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
0

Answer by itsharshdeep · Oct 07, 2015 at 04:31 AM

Hi @Klaster55

First of all the script you above wrote contains the 'OnGUI' method which run with every frame (same as Update works)

So if the script is enable it will run automatically without your permission.

Now suppose you want to print the random Number when you click on Button, Then just do the following steps :-

 int randomNum;
 public  void CreateRandomNumber() {
            randomNum = Random.Range (0,100);
            print("The number that was generated: " + randomNum);
      }

Now pass this method in 'OnClick()' property it will print a log in console.

If this answer is not appropriate to your question then please try to describe with little more description in comment. Thanks

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

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

32 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

Related Questions

Button OnClick listener with unsigned integer argument 0 Answers

Cannot assign variable agents for clicking a button 0 Answers

How do I can use Arduino push button trigger unity UI button (on click ()) , no direct active effect? code can function but it had duplicate code and not the result that I want. 0 Answers

Dymanic buttons with different openURL links 0 Answers

multiple buttons activating at the same time, I'm at my wits end 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