• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Tobias · Jun 16, 2010 at 12:26 AM · guirpc

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();
        }
}
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Tobias · Jun 16, 2010 at 12:53 AM 0
Share

Thanks but I get the following error: $$anonymous$$issing$$anonymous$$ethodException: $$anonymous$$ethod not found: 'System.Object.FindObjectOfType'.

avatar image Tobias · Jun 16, 2010 at 12:58 AM 0
Share

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?

avatar image Mike 3 · Jun 16, 2010 at 08:07 AM 0
Share

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)

avatar image Tobias · Jun 16, 2010 at 09:54 AM 0
Share

Thank you very much, works perfectly :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

No one has followed this question yet.

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges