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 /
This question was closed May 20, 2019 at 12:35 PM by Bunny83 for the following reason:

Question is outdated. UnityScript is no longer supported

avatar image
0
Question by The Boy · Aug 19, 2014 at 01:07 AM · c#javascriptvoid

Calling a C# function from a Javascript

I have 3 scripts: GameControl.js, DataManager.cs and GamePause.js.

The GameControl script is in the folder "Plugins".

 //GameControl.js
 public var currentXP : int = 0;
 public var maxXP : int = 10;
 public var currentLevel : int = 1;
 
 public var currentHealth : float = 45;
 public var maxHealth : int = 45;
 
 public var currentEnergy : float = 27;
 public var maxEnergy : int = 27;
 
 public var currentStrengh : int = 15;
 private var maxStrengh : int = 650;
 
 public var currentAttack : int = 11;
 private var maxAttack : int = 670;
 
 public var currentDefense : int = 20;
 private var maxDefense : int = 760;
 
 private var currentStamina : float = 25;
 public var maxStamina : int = 25;
 
 public var currentCritical : int = 5;
 private var maxCritical : int = 100;
 
 public var currentSpeed : int = 12;
 private var maxSpeed : int = 690;
 
 public var currentHP : float = 15;
 public var maxHP : int = 15;
 
 //more stuff...

The DataManager script makes like a Save and Load System, and is located in a normal folder, but this need some variables from GameControl script.

 //DataManager.cs
 using UnityEngine;
 using System.Collections;
 
 public class DataManager : MonoBehaviour
 {
     public static DataManager dataSaveAndLoad;
 
     GameControl dataVariables;
 
     float healthData;
     int maxHealthData;
     int experienceData;
     int maxExperienceData;
     int levelData;
     float energyData;
     int maxEnergyData;
     int attackData;
     int strenghData;
     int defenseData;
     int maxStaminaData;
     int criticalData;
     int speedData;
     float HPData;
     int maxHPData;
 
     void Start ()
     {
         dataVariables =    GameObject.Find("Player").GetComponent<GameControl>();
     }
 
     void Update ()
     {
         healthData = dataVariables.currentHealth;
         maxHealthData = dataVariables.maxHealth;
         experienceData = dataVariables.currentXP;
         maxExperienceData = dataVariables.maxXP;
         levelData = dataVariables.currentLevel;
         energyData = dataVariables.currentEnergy;
         maxEnergyData = dataVariables.maxEnergy;
         attackData = dataVariables.currentAttack;
         strenghData = dataVariables.currentStrengh;
         defenseData = dataVariables.currentDefense;
         maxStaminaData = dataVariables.maxStamina;
         criticalData = dataVariables.currentCritical;
         speedData = dataVariables.currentSpeed;
         HPData = dataVariables.currentHP;
         maxHPData = dataVariables.maxHP;
     }
 
     public void Save()
     {
         //...
     }
     
     public void Load()
     {
         //...
     }
 }

The problem is... How to call a C# function/void from a Javascript? The GamePause have some GUIButtons, and from this script call the DataManager functions/voids.

 //GamePause.js
 private var isPause : boolean = false;
 private var pauseWindow = new Rect (20,20,300,150);
 
 function Start ()
 {
     pauseWindow = new Rect (20,20,300,150);
 }
 
 function Update ()
 {
     if(Input.GetKeyDown(KeyCode.Escape))
     {
         isPause = !isPause;
         if(isPause == true)
         {
             Time.timeScale = 0;
             Screen.showCursor = true;
         }
         if(isPause == false)
         {
             Time.timeScale = 1;
             Screen.showCursor = false;
         }
     }
 }
 
 function OnGUI ()
 {
     if(isPause == true)
     {
         pauseWindow = GUI.Window(0,pauseWindow,PauseMenu,"Pausa");
         GUI.Box(Rect(0,0,Screen.width,Screen.height),"");
     }
     
     if(isPause == false)
     {
         Time.timeScale = 1;
         Screen.showCursor = false;
     }
 }
 
 function PauseMenu ()
 {
     if(GUILayout.Button("Continue"))
     {
         isPause = false;
     }
     GUILayout.Button("Save"); //Call Save void
     GUILayout.Button("Load"); //Call Load void
     GUILayout.Button("Game Settings");
     if(GUILayout.Button("Main Menu"))
     {
         Application.LoadLevel("MainMenu");
     }
 }
Comment
Comments Locked
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

3 Replies

  • Sort: 
avatar image
0

Answer by Cherno · Aug 19, 2014 at 01:20 AM

Ther should be no issue. You call the C# function just like you would call a UnityScript function.

 GetComponent(C#Script).C#FunctionName();
Comment
Comments Locked · Show 2 · 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 The Boy · Aug 19, 2014 at 01:27 AM 0
Share

Doesn't work. BCE0018: The name 'Data$$anonymous$$anager' does not denote a valid type ('not found').

avatar image _13PhysX · Apr 29, 2016 at 06:10 AM 0
Share

BCE0005: $$anonymous$$ identifier: '$$anonymous$$uteSound'.

avatar image
0

Answer by Kiwasi · Aug 19, 2014 at 01:45 AM

Communicating between the two languages is painful, and best avoided.

It relies a lot on script compilation order. The script to be accessed (in this case DataManager) must be compiled first. So put DataManager in the plugins folder. Put GameControl and PauseControl in standard folders. Things should all work then.

But a better solution would be to rewrite all scripts into your chosen language.

Comment
Comments Locked · 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
0

Answer by DBar · Aug 19, 2014 at 01:56 AM

Please USE SendMessage with params.

This is the correct way to call a Remote Procedure Call.

Other solution for this is using just one languaje but this will do.

Dont mess with putting scripts in plugins and stuff, it is HELL.

Learn this and suffer no more:

http://docs.unity3d.com/ScriptReference/Component.SendMessage.html

Comment
Comments Locked · 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

Follow this Question

Answers Answers and Comments

27 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

Related Questions

Multiple Cars not working 1 Answer

C# Unity 3D Lock Rotation but allow Rotation of Parent Object 1 Answer

Can a script be loaded to a next scene? 3 Answers

Convert this line of javascript to C# (easy) 1 Answer

Translate code from C# to Javascript 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