- Home /
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?
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();
}
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!
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);
}
}
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
Follow this Question
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