• 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
Question by silverxah · Sep 05, 2017 at 12:30 AM · uibuttonmultiplayercanvasslider

Call Functions On Player From Canvas Elements In a Multiplayer Game

So I was originally using the singleton idea to call functions on the player but then I quickly realized that that wouldn't work in an online multiplayer setting, so how do I go about trying to call functions from buttons and sliders on the canvas?

Comment

People who like this

0 Show 0
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

Answer by ooblii · Sep 05, 2017 at 01:38 AM

One way might be to have a singleton GameManager class cache a reference to the local player’s gameObject (I’m assuming you only want the button to affect the local player), then use that reference in whatever class is handling your UI methods.

 public void OnButtonClick()
 {
         GameManager.Instance.LocalPlayer.GetComponent<SomeClass>().DoSomet$$anonymous$$ng();
 }

Comment
silverxah

People who like this

1 Show 3 · 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 silverxah · Sep 13, 2017 at 12:42 AM 0
Share

Well I managed to get it working in a multiple camera setup (each player has their own child camera) by having the singleton methods inherit from NetworkBehaviour, but when I switched to using a single camera (having a single main camera and having that camera follow the local player) set up, the whole thing stopped working so I guess I'll try use the GameManager cache method. Thanks!

avatar image silverxah · Sep 13, 2017 at 04:34 AM 0
Share

Quick question, some of my button's open up other options (basically setting inactive/active certain panels) and that seems to be the part that is not working working for the singleton methods. How would I do this via a GameManager Instance?

 public class MobileGuiController : NetworkBehaviour
 {
 
     public Button camMode;
 
     public Button cutMode;
 
 
 
     public GameObject camControls;
 
     public GameObject cutControls;
 
     public static MobileGuiController singleton = null;
 
     void Awake()
     {
         if (singleton != null)
         {
             Destroy(gameObject);
             return;
         }
 
         singleton = this;
         DontDestroyOnLoad(this);
     }
 }

 public class MobileGui : NetworkBehaviour
 {
     //why does this not work when i'm not host
 
     // Use this for initialization
 
     private MobileGuiController control;
 
 
     private Button camMode;
 
     private Button cutMode;
 
 
     private GameObject camControls;
 
     private GameObject cutControls;
 
  void Start()
     {
         //#if (UNITY_IOS || UNITY_ANROID) //temporarily disabled preprocessing checking for testing, re-enable when building to mobile
         control = MobileGuiController.singleton;
 
         camMode = control.camMode;
         camMode.onClick.AddListener(() => ToggleCamMode());
        
 
         cutMode = control.cutMode;
         cutMode.onClick.AddListener(() => ToggleCutMode());
 
         camControls = control.camControls;
         cutControls = control.cutControls;
 }
 public void ToggleCamMode()
     {
         mode = 1;
         mode2 = mode;
         camControls.SetActive(!camControls.activeSelf);
     }
 
     public void ToggleCutMode()
     {
         mode = 2;
         mode2 = mode;
         cutControls.SetActive(!cutControls.activeSelf);
     }
 }


avatar image ooblii silverxah · Sep 13, 2017 at 02:11 PM 0
Share

This seems a little redundant. Why not put your two toggle methods in your MobileGuiController? If the MobileGuiController class is attached to your main canvas, the canvas will survive scene changes and you can hook up your OnClick listeners in the editor.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

157 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 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 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 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 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

UI Button onclick not detected 0 Answers

button OnClick function triggering another buttons OnClick 0 Answers

Joystick stops working after scene is restarted from UI button 1 Answer

My UI buttons work fine in editor but when I exported the game on Android, only some of them works. 0 Answers

UI 4.6 canvas button, how to make it invisible on gameplay and how to make it visible when player is dead? 3 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