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 atom182 · Apr 12, 2016 at 04:26 PM · buttononclick

question about buttons and event onclick

Hi i'am new in unity 3D, and i work in a project and i need do the next things with buttons and i don't know how to do this.

i have a button empty, when i click the button change the text of the button to "X", in the next click i change the text to "O", in the next click change to "I" and finally in the next click return to button empty

i don't know how to program the event and the button. alt text

sin-titulo.png (325.3 kB)
Comment
Add comment · Show 3
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 YOLO_cakes · Apr 12, 2016 at 05:23 PM 0
Share

What exactly are you asking for? Do you want the button to be blank and then change to the letter "X" or does X mark a word. Or do you want the button to do something like start the game. It may just be me but I am slightly confused.

avatar image atom182 · Apr 18, 2016 at 04:41 PM 0
Share

ok now i understand the event onlick... i only have the code to change the text in te button. but i have the error in the line " public Text ButText; " the type or name space 'text' could not be find. why???

i have this code

 public Text ButText;
 int counter = 0;
 
 public void changeText()
 {
     counter += 1;
     switch (counter)
     {
         case 1:
             ButText.text = "X";
             break;
         case 2:
             ButText.text = "O";
             break;
         case 3:
             ButText.text = "I";
             break;
         default:
             counter = 0;
             ButText.text = "";
             break;
     }
 }

}

and i can't use the function in onclick(), because don't gave me the function changeText().

avatar image atom182 · Apr 19, 2016 at 11:21 PM 0
Share

i don't understand... i attach the script ti all my buttons, and drop the combioBoton???

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Ali-hatem · Apr 12, 2016 at 05:52 PM

 Text ButText; // edited
 int counter = 0;
 void Start(){
     ButText = GetComponentInChildren<Text>(); // add this line
     ButText.text = "";
 }

 public void change(){
     counter += 1;
     switch (counter) {
     case 1:
         ButText.text = "X";
         break;
     case 2:
         ButText.text = "O";
         break;
     case 3:
         ButText.text = "I";
         break;
     default :
         counter = 0;
         ButText.text = "";
         break;
     }
 }
Comment
Add comment · Show 10 · 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 atom182 · Apr 15, 2016 at 08:27 PM 0
Share

i have this code... but i don't understand the public Text ButText; this variable is the name of my button or what is this? for example public button1 ButText; ???

using UnityEngine; using System.Collections;

public class botonOnClick : $$anonymous$$onoBehaviour {

 public Text ButText;
 int counter = 0;
 void Start()
 {
     ButText.text = "";
 }

 void OnGUI()
 {
     Event e = Event.current;
     if (e.button == 0 && e.is$$anonymous$$ouse)
     {
         Debug.Log("Left Click");
         change();
         }
     else
         if (e.button == 1)
         Debug.Log("Right Click");
     else
             if (e.button == 2)
         Debug.Log("$$anonymous$$iddle Click");
     else
                 if (e.button > 2)
         Debug.Log("Another button in the mouse clicked");
 }

 void change()
 {
     counter += 1;
     switch (counter)
     {
         case 1:
             ButText.text = "X";
             break;
         case 2:
             ButText.text = "O";
             break;
         case 3:
             ButText.text = "I";
             break;
         default:
             counter = 0;
             ButText.text = "";
             break;
     }
 }

}

avatar image Ali-hatem atom182 · Apr 16, 2016 at 08:26 AM 0
Share

public Text ButText; // ButText is the child object if the Button . & you asked for buttons and event onclick so why you use Event e = Event.current; all you need is create onclick event on each button & call the change function & i already tested it in unity & it work so if you don't know how to use onclick see this tutorial make buttons do something

avatar image atom182 Ali-hatem · Apr 18, 2016 at 03:02 PM 0
Share

ok now i understand the event onlick... i only have the code to change the text in te button. but i have the error in the line " public Text ButText; " the type or name space 'text' could not be find. why???

i have this code

  public Text ButText;
  int counter = 0;
  
  public void changeText()
  {
      counter += 1;
      switch (counter)
      {
          case 1:
              ButText.text = "X";
              break;
          case 2:
              ButText.text = "O";
              break;
          case 3:
              ButText.text = "I";
              break;
          default:
              counter = 0;
              ButText.text = "";
              break;
      }
  }

}

and i can't use the function in onclick(), because don't gave me the function changeText().

Show more comments
Show more comments
avatar image Ali-hatem atom182 · Apr 19, 2016 at 10:16 PM 0
Share

delete the combioBoton object because it handles one button text so ins$$anonymous$$d we will use the buttons as game objects so attach the script to the buttons all the buttons will use the same script & on each button create On Click Event & drag & drop the each button to it's on On Click Event it's like what you have done with combioBoton object but we replaced it with the buttons . i see you tomoro it's 1 A$$anonymous$$ god night.

avatar image atom182 Ali-hatem · Apr 25, 2016 at 09:02 PM 0
Share

i don't know if i'am bad or stupid... i attach the script in the button and i add the button in the event onclick(). i select the option botonOnClick.changeText(); and mark the next error "nullReferenceException: Object reference not set to an object"

i drop the combioBoton. alt text

sin-titulo3.png (277.5 kB)
Show more comments
avatar image
0

Answer by Ryanless · Apr 19, 2016 at 11:30 PM

you have to make a reference to the ButtonText:

if you have public Text buttontext, then just drag and drop the text Gameobject on the field in the inspector. Or u could use ButText = GetComponentInChildren(); as mentioned. The good thing about that is it finds it automaticlly provided the script is on the button.

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

6 People are following this question.

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

Related Questions

My onclick action listeners I attach to my buttons as I instantiate them only work once 1 Answer

OnClick() problem with Button script: "Deleting an array element will copy the complete array to all..." 1 Answer

Reference For Button Which Calls OnClick 4 Answers

onclick.addlistener only works once 0 Answers

button.onClick.AddListener(method); NOT Working 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