- Home /
Problem with RPC - GUI
Hello,
I have an Account Creation GUI where I enter Username, password ,...
then I use a RPC to tell the server to insert the Account in the database and then Send an RPC back with Successfull or not.
This is working perfectly!
But I dont want to have my RPCs in my GUI Script. So I wanted to put the RPCs to another script.
The Registration Successfull RPC is calling a function to open a GUI Popup, so I need to use "AddComponent" on my RPC Gameobject. Ok now to the problem: When I use AddComponent("MyGUIScript") I have 2 GUI's! :( But I only want to use a function in MyGUIScript and not to have a completly new GUI. How to do this? Making the function static will not solve the problem because it is calling other functions in the GUI, and making all functions static isnt very fine.
So my question: How to call a Function in a GUIScript without using AddComponent, because this will create a second GUI?
Thank you very much.
Here is a little code Snippet:
Send this to server
@RPC
function CreateAccount (username : String, password : String, email : String) {}
Get this from the server and Call RegistrationSuccessfull in the GuiScript (In this example the RPC is in the GUI so I can call RegistrationSucessfull without using AddComponent. But this doesnt work when I put the RPC into another Script.
@RPC
function CreationStatus (status : int){
//Successfull
if(status == 1){
RegistrationSucessfull();
}
}
Answer by Mike 3 · Jun 16, 2010 at 12:46 AM
You can use
var script : MyGUIScript = UnityEngine.Object.FindObjectOfType(MyGUIScript);
then you can call functions on it as you like
Another way to do it would be to use the singleton pattern - check this page on the wiki for ideas:
http://www.unifycommunity.com/wiki/index.php?title=Singleton
Edit:
for making sure you have a component added to the same gameobject, add this above the class definition:
c#
[RequireComponent(typeof(MyGUIScript))]
js
@script RequireComponent(MyGUIScript)
http://unity3d.com/support/documentation/ScriptReference/RequireComponent.html
Thanks but I get the following error: $$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'System.Object.FindObjectOfType'.
Im now using var script : GUI$$anonymous$$ain$$anonymous$$enue; script = gameObject.GetComponent(GUI$$anonymous$$ain$$anonymous$$enue);
This is working really good. But is it possible to set a require? So that you cant attach the script to an object if the Component isnt attached? Or that the GUIscript gets automatic attached when I attach the RPC script?
yup yup it is. will edit my post (and to fix the other problem you had, just in case ou need to use it again)
Your answer
Follow this Question
Related Questions
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Setting Scroll View Width GUILayout 1 Answer
Bigger size button help 2 Answers
Any problems with this script? 1 Answer
Card matching game 1 Answer