Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • 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
2
Question by Chapi · Oct 26, 2014 at 06:30 PM · c#javascriptfunctioncalling

Calling JS Static Function from C# Script

I'm sorry I'm asking a question about something that has been asked a thousand times, I've read a couple other answers but none of them helped me. I have a script called php.js which has a publiic static function inside it called login. From my C# script I am trying to call this function, both scripts are attached to the same gameobject.

I've already put the php.js on first pass compile by putting it inside plugins. On VS it does recognize php when I use it on php PHPSCRIPT = GetComponent(); And then on the function that should be calling the login() I have PHPSCRIPT.login();

No compile errors or warnings, it is just not working, I added a debug.log("Called") inside the login() to check if it is getting called but it is not showing up Called so it is not getting called. What am I doing wrong?

php.js #pragma strict

 public  var userID : int;
 public  var hasLogged : boolean = false;
 
 public function PHPLOGIN (user : String, password : String)
     {
     Debug.Log("Called2");
     var url = "http://www.****.com/unity.php?username="+user+"&password="+password;
     var w = WWW(url);
     print(w.url);
     yield w;
     if (w.error != null)
         {
         print(w.error);
         userID = 0;
         }
     else
         {
         var formText = w.text;
         print(w.text);
         userID = int.Parse(formText);
         w.Dispose();
         if (userID > 0)
             {
             hasLogged = true;
             Debug.Log("Logged in with ID: " + userID);
             }
         }
     }


networkingScript.cs

 public class networkingScript : MonoBehaviour 
 {
 public php PS;
 void Start()
     {
         PS = GetComponent<php>();
     }
     public void logIn(string user, string password)
     {
         PS.PHPLOGIN(user, password);
         Debug.Log("Called");
     }
 }
Comment
Add comment · Show 2
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 tanoshimi · Oct 26, 2014 at 06:58 PM 0
Share

We'd need to see code.

avatar image Chapi · Oct 26, 2014 at 07:37 PM 0
Share

There it is

1 Reply

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

Answer by Bunny83 · Oct 26, 2014 at 08:10 PM

Your PHPLOGIN function is a coroutine. You have to use StartCoroutine in C#. UnityScript aitomagically does this for you when you "call" a coroutine. However i would suggest to wrap the coroutine thing in the UnityScript class.

Your naming makes it really difficult to read the code. Some general coding rules:

  • Class and method names always start with a capital letter. You usually use the "UpperCamelCase".

  • Identifiers that are ALLCAPS are usually constants. Normal variable names should use the "lowerCamelCase".

  • Use class names that describe what the class is good for. Don't use too long class names, but at least one that's more descriptive.

So to solve your problem:

 // php.js
 public var userID : int;
 public var hasLogged : boolean = false;
 private function LoginCoroutine (user : String, password : String)
 {
     var url = "...";
     var w = WWW(url);
     yield w;
     // [...]
 }
 
 public function Login(user : String, password : String)
 {
     LoginCoroutine(user, password);
 }

In your C# script:

 public void LogIn(string user, string password)
 {
     PS.Login(user, password);
 }

The shortest fix (if you want to ignore my naming advice) is to use StartCoroutine. So in your original C# script just do

 StartCoroutine(PS.PHPLOGIN(user, password));

instead of

 PS.PHPLOGIN(user, password);

That should do it.

Comment
Add comment · Show 3 · 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 Bunny83 · Oct 26, 2014 at 08:14 PM 1
Share

Ohh, i just read your title again... The method in your UnityScript file is not "statis". I'm not sure why you wrote that. Do you know what static means?

Also as last advice: Try to avoid mixing languages! Otherwise you'll get into trouble in the long run. It might work in some small / simple applications or games but for bigger projects, pick one language.

avatar image Chapi · Oct 26, 2014 at 08:19 PM 0
Share

Thanks alot for the great detailed answer :) About the static thing, at first I had at as static which made me think that could be the problem, so I made it non-static (instanced) but it changed nothing that's why I left it like that by accident hah. Also about the na$$anonymous$$g, yes you're right, that part of my code has messed up na$$anonymous$$g, I usually do it like that at first to build the code quick, then I make it work and pulish it and then figure out correct names :P.

And last, yeah I hate mixing languages too, I really don't want to handle JS but since I know nothing about php and about connecting to a webserver using unity I was forced to use JS since I couldn't find a tutorial on it which used C# and I don't know anything about co-routines neither so, yeah I was forced to :/.

avatar image Chapi · Oct 26, 2014 at 08:25 PM 0
Share

And again thank you very much :) I did what you said about making a auxiliary function which started the coroutine function on the JS script and it worked perfectly :D. I'm just waiting until I know enough php and co-routines in C# to work out a C# php script by my-self.

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.

Follow this Question

Answers Answers and Comments

29 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to call a c-sharp function from javascript? 1 Answer

Question about classes [will specify once I know what the name of what I'm looking for is] 0 Answers

Sending a message from javascript to C# 1 Answer

get component javascript from a c# script 3 Answers

What is this C# code in javascript 0 Answers


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