- Home /
How to call a function from another script?
Okay so I have a script called MenuManager and I'm trying to tell it to run the function StartServer in my MultiplayerManager script. obviously I'm new to this and I'm following a tutorial but this scripts are in c# and I'm recreating them in JavaScript. I have tried searching for this already but couldn't solve it. so here are the two scripts: (im trying to call the function on line 53)
// Variables
var CurrentMenu;
var MatchName : String;
var MatchPassword : String;
var MatchMaxPlayers : int = 32;
function Start ()
{
CurrentMenu = "Main";
}
function OnGUI()
{
if(CurrentMenu == "Main")
Menu_Main();
if(CurrentMenu == "Lobby")
Menu_Lobby();
if(CurrentMenu == "Host")
Menu_HostGame();
}
public function NavigateTo(nextmenu)
{
CurrentMenu = nextmenu;
}
private function Menu_Main()
{
if (GUI.Button(new Rect(10,10,200,50), "Host Game"))
{
NavigateTo("Host");
}
}
private function Menu_HostGame()
{
//Buttons Host Game
if (GUI.Button(new Rect(10,10,200,50), "Back"))
{
NavigateTo("Main");
}
// Start Server Button
if (GUI.Button(new Rect(10,60,200,50), "Start Server"))
{
// THIS IS WHERE I WANT TO CALL IT FROM
MultiplayerManager.Instance().StartServer;
}
//Text boxes and lables
GUI.Label(new Rect(220, 10, 130, 30), "Match Name");
MatchName = GUI.TextField(Rect(400, 10, 200, 30), MatchName);
GUI.Label(new Rect(220, 50, 130, 30), "Match Password");
MatchPassword = GUI.PasswordField(Rect(400, 50, 200, 30), MatchPassword, "*"[0],25);
GUI.Label(new Rect(220, 90, 130, 30), "Maximum Players");
GUI.Label(new Rect(400, 90, 200, 30), MatchMaxPlayers.ToString());
MatchMaxPlayers = Mathf.Clamp(MatchMaxPlayers, 8, 32);
if (GUI.Button(new Rect(425,90,25,30), "+"))
{
MatchMaxPlayers += 2;
}
if (GUI.Button(new Rect(450,90,25,30), "-"))
{
MatchMaxPlayers -= 2;
}
}
private function Menu_Lobby()
{
}
And here is the MultiplayerManager script:
public class MultiplayerManager extends MonoBehaviour
{
private static var instance:MultiplayerManager;
public static function Instance():MultiplayerManager
{
return instance;
}
public function StartServer()
{
}
}
Well currently it just says the "Expressions in statements must only be executed for their side-effects."
Also when currently i can access the $$anonymous$$ultiplayer$$anonymous$$anager from the $$anonymous$$enu$$anonymous$$anager but thats as far as it goes, i dont know how to call the function after that
Answer by Ziethriel · Jun 15, 2013 at 12:42 AM
You're trying to call two functions from the same action. The error is "Expressions in statements must only be executed for their side-effects." because you don't have () behind the second function.
Right now your dot syntax suggests that StartServer is a part of the function Instance(). Try This:
// THIS IS WHERE I WANT TO CALL IT FROM MultiplayerManager.Instance(); MultiplayerManager.StartServer();
Cheers for that, i just changed it to $$anonymous$$ultiplayer$$anonymous$$anager.StartServer(); and then changed StartServer to static and it works perfectly! thanks alot
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
setup a php master server 2 Answers
Adding force to a projectile? (SOLVED) 1 Answer
How do I add gravity to my object, and how do I fix my network problem? 1 Answer