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
0
Question by imnickb · Mar 25, 2013 at 02:50 AM · c#2djavascriptvariableaccess

Access a JavaScript variable from a C# script?

I just started integrating the 2D toolkit into a little demo I've been working on. I've been doing everything in JavaScript but the 2DToolkit stuff all seems to be in C#. The scenario is I have a Hero(JS) who detects collision with coins, and I need to increment the score counter (C#). I've tried somethings and haven't been able to make it work. Here's my HUD script:

 using UnityEngine;
 using System.Collections;
 
 public class TextMeshExample : MonoBehaviour {
 
     tk2dTextMesh textMesh;
     int score = 0;
 
     // Use this for initialization
     void Start () {
         textMesh = GetComponent<tk2dTextMesh>();
     }
 
     // Update is called once per frame
     void Update () {
         //replace with something like textMesh.text = "SCORE: " HeroController.coinCount
         if (Input.GetKey(KeyCode.Q))
         {
             score++;
 
             textMesh.text = "SCORE: " + score.ToString();
 
             // This is important, your changes will not be updated until you call Commit()
             // This is so you can change multiple parameters without reconstructing
             // the mesh repeatedly
             textMesh.Commit();
         }
     }
 }


and here's part of my hero script:

 function OnCollisionEnter(collision : Collision) {
     if(collision.gameObject.name == "WaterBarrier") // If A collides with B
     {
         if (collision.relativeVelocity.magnitude > outOfWaterThreshold/100)
         {
             //Physics.gravity = Vector3(0, -1.5, 0);
             //transform.position = Vector3(-0.6948604, .5, 3.701174);
             print("We just hit the water level... Hard!!!");
             //Physics.gravity = Vector3(0, 3, 0);
             //transform.position = Vector3(-0.6948604, -.5, 3.701174);
         }
     }
     
     //If we hit a coin increment the count and destroy it
     if(collision.gameObject.name == "Coin" || collision.gameObject.name == "SilverCoin") 
     {
         transform.position = Vector3(transform.position.x, transform.position.y, 3);
         Destroy(collision.gameObject);
     
         if(collision.gameObject.name == "Coin")
         {
             coinCount = coinCount+1;
         }
         else if(collision.gameObject.name == "SilverCoin")
         {
             coinCount = coinCount+2;
         }
     }
 
     //If we hit a hazard increment the count and restart the level
     if(collision.gameObject.name == "Hazard")
     {
         Application.LoadLevel ("ProofOfConcept");
         hazardCount = hazardCount+1;
     }
 }


I'm trying my best, so if somebody could give me some insight I'd really appreciate it! Thanks so much!

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

2 Replies

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

Answer by imnickb · Mar 25, 2013 at 07:30 PM

A little more info on this in case anyone's in the same boat as me; looking to use 2DToolKit with JavaScript. The 2DToolKit itself is scripted using C# so there does need to be some communication between JavaScript and C# if you wish to script in JavaScript. You need to adjust where some things are located and in what order things are compiled to get this to work, but the good news is that Unikron has set up the 2DToolKit to take care of all that for you. Just go to the 2DToolKit menu and select "Setup for JavaScript." After that you can do your 2DToolKit scripting in JavaScript, you just need to adapt the syntax in some of the tutorials since they're all written in C#. I hope this might help someone in the future.

http://unikronsoftware.com/2dtoolkit/forum/index.php/topic,1381.0.html

Comment
Add comment · 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
2

Answer by nsxdavid · Mar 25, 2013 at 05:11 AM

JavaScript and C# have difficulty talking to each other because of the order of compilation.

Opinion: Use C# please. UnityScript is the devil.

More detail: http://docs.unity3d.com/Documentation/ScriptReference/index.Script_compilation_28Advanced29.html

Comment
Add comment · Show 1 · 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 _invalidusername · Sep 08, 2015 at 05:05 PM 0
Share

Amen to that

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

12 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

Related Questions

Can't access a javascript static variable from c# script 1 Answer

Need to use 2 different language scripts. 1 Answer

How do you change a variable in a script, that is on an object instantiated from a prefab? 0 Answers

Destroy Object On Collision? 3 Answers

Access and Change variables from other Scripts? 2 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