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

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>().DoSomething();
 }

Comment
Add comment · 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 Game$$anonymous$$anager 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 Game$$anonymous$$anager Instance?

 public class $$anonymous$$obileGuiController : NetworkBehaviour
 {
 
     public Button cam$$anonymous$$ode;
 
     public Button cut$$anonymous$$ode;
 
 
 
     public GameObject camControls;
 
     public GameObject cutControls;
 
     public static $$anonymous$$obileGuiController singleton = null;
 
     void Awake()
     {
         if (singleton != null)
         {
             Destroy(gameObject);
             return;
         }
 
         singleton = this;
         DontDestroyOnLoad(this);
     }
 }

 public class $$anonymous$$obileGui : NetworkBehaviour
 {
     //why does this not work when i'm not host
 
     // Use this for initialization
 
     private $$anonymous$$obileGuiController control;
 
 
     private Button cam$$anonymous$$ode;
 
     private Button cut$$anonymous$$ode;
 
 
     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 = $$anonymous$$obileGuiController.singleton;
 
         cam$$anonymous$$ode = control.cam$$anonymous$$ode;
         cam$$anonymous$$ode.onClick.AddListener(() => ToggleCam$$anonymous$$ode());
        
 
         cut$$anonymous$$ode = control.cut$$anonymous$$ode;
         cut$$anonymous$$ode.onClick.AddListener(() => ToggleCut$$anonymous$$ode());
 
         camControls = control.camControls;
         cutControls = control.cutControls;
 }
 public void ToggleCam$$anonymous$$ode()
     {
         mode = 1;
         mode2 = mode;
         camControls.SetActive(!camControls.activeSelf);
     }
 
     public void ToggleCut$$anonymous$$ode()
     {
         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 $$anonymous$$obileGuiController? If the $$anonymous$$obileGuiController 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.

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

Problems with render priority in UI Canvas 2 Answers

Can't click or highlight a button. 0 Answers

Can't interact with buttons in a UI Canvas in World Space Render Mode. 0 Answers

How to make a UI Canvas not appear again after the first time the user hits the "I Agree" button. 2 Answers

Buttons in UI Scroll Rect 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