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

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