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
1
Question by boowman · Dec 22, 2014 at 07:32 AM · uibuttononclick

Change variable value using new UI Button

Basically what I am trying to achieve is changing the variable value when I click a button. This is the code where I change the variable through a function

 using UnityEngine;
 using UnityEngine.UI;
 using System.Collections;
 
 public class LocalPlayer : MonoBehaviour
 {
     //Define All Classes
     public int playerClass;
 
     public void RunnerClassValue() { playerClass = 1; }
     public void KillerClassValue() { playerClass = 2; }
     public void SpectatorClassValue() { playerClass = 3; }
 
     void Update()
     {
         Debug.Log("PlayerClass: " + playerClass);
     }
 }

Image with the button to see that I set the correct function alt text

Basically the problem is that in the update the Debug keeps display 0 instead of 1 or 2 or 3 after I click the button. If I add a debug in the function it displays the function value but not in update.

Comment
Add comment · Show 1
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 fafase · Dec 22, 2014 at 09:26 AM 0
Share

Works fine with me. Copy pasted your code and I get what should be. On the other hand, if you added two of that scripts, one of them will display 0.

2 Replies

· Add your reply
  • Sort: 
avatar image
1

Answer by kilijouli · Mar 11, 2016 at 02:57 PM

it's been more than a year i know, but i actually was facing the same problem until very recently. So i'm posting this to anyone who, like me, is having trouble with that. I Actually used the SendMessage() function to call another function in the script attached to the game object i was trying to access. So here's my suggestion.

You should add the following script to your button prefab:

 **using** UnityEngine;
 **using** System.Collections;
 **using** UnityEngine.UI;
 
 public class ButtonTest : MonoBehaviour {
 
     Button button = null;
     string tag;
 
 
     void Start () 
     {
         button = gameObject.GetComponent<Button>();
         button.onClick.AddListener(() =>{Click();});
         tag = transform.tag;
     }
     
 
     void Click () 
     {
         switch (tag) 
         {
         case "Runner":
             GameObject.FindGameObjectWithTag("player").SendMessage ("RunnerClassValue");
             break;
         case "Killer":
             GameObject.FindGameObjectWithTag("player").SendMessage ("KillerClassValue");
             break;
         case "Spectator":
             GameObject.FindGameObjectWithTag("player").SendMessage ("SpectatorClassValue");
             break;
         }    
     }
 }

so basically the line GameObject.FindGameObjectWithTag("player").SendMessage ("SpectatorClassValue"); will look for any game object with the tag "player", look in his components for the functions put as argument in the SendMessage and call them.

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 boowman · Dec 22, 2014 at 12:47 PM

Ok so I just found out why it doesn't work. Basically LocalPlayer script is attached to a PlayerPref which will get instantiated when I press Play. Any way to fix that?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

How to ADD one Buttons.onClick events to another Buttons.onClick? 1 Answer

How to access the OnClick event via Script 1 Answer

Button.onClick.AddListener inconsistent 0 Answers

How to change the Object on UI button 1 Answer

button OnClick function triggering another buttons OnClick 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