- Home /
The question is answered, right answer was accepted
Calling variables and functions
I made these scripts when i was using unity 3, now i'm using unity 4, but i can't understand why this isnt working now? This worked before updating...
var multiplayerscript;
multiplayerscript = GameObject.Find("Networking").gameObject.GetComponent(CoOp);
And I use this for things like this:
multiplayerscript.startServer();
Error message for that was: Assets/Scripts/Menu/Menu.js(14,19): BCE0019: 'startServer' is not a member of 'Object'.
I dont think you need this part of networking script, but i'll show it too. So I was calling this function:
function startServer(){
Network.InitializeServer(2,menuscript.remotePort, !Network.HavePublicAddress);
MasterServer.RegisterHost(gameName, "blabla", "blabalabal");
}
GameObject.Find returns a GameObject. the above code does a: gameObject.gameObject.GetComponent(). you can remove the middle "gameObject" part. I do not know javascript well so that is the other thing that is glaring to me.
Answer by flamy · Nov 30, 2012 at 01:14 PM
var multiplayerscript:CoOp;
multiplayerscript = (CoOp) GameObject.Find("Networking").gameObject.GetComponent(CoOp);
multiplayerscript.startServer();
This one should work. Declaring as var multiplayerscript will create object of Object class that holds any datatype. Sp always specify the class name while declaring.
The casting in the second line is just in case if it doesnt work. it wont be error if u remove it too.
No errors now, but when I press start server and run that startServer function, it shows this:
InvalidCastException: Cannot cast from source type to destination type. Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (IConvertible convertible) Boo.Lang.Runtime.RuntimeServices.CheckNumericPromotion (System.Object value) Boo.Lang.Runtime.RuntimeServices.UnboxInt32 (System.Object value) CoOp.startServer () (at Assets/Scripts/Co-op/CoOp.js:14) $$anonymous$$enu.OnGUI () (at Assets/Scripts/$$anonymous$$enu/$$anonymous$$enu.js:14)
I can't understand what is wrong with these lines..
CoOp line 14: Network.InitializeServer(2,menuscript.remotePort, !Network.HavePublicAddress);
$$anonymous$$enu line 14: multiplayerscript.startServer();
Network.InitializeServer(2,menuscript.remotePort, !Network.HavePublicAddress());
HavePublicAddress is an function u missed (). generally in unity what ever name starts with Capital, it will be function of a class and others are variables of the class.
Try with:
multiplayerscript = GameObject.Find("Networking").GetComponent(CoOp);
Follow this Question
Related Questions
Unity networking tutorial? 6 Answers
Main Parameters of MP game for Clients to Join 0 Answers
Multiplayer pause menu won't disappear 0 Answers
Network Tutorial Interpolation - TPS Auth 0 Answers
The magic key to networking? 0 Answers