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 Mint92 · Feb 10, 2014 at 11:36 AM · c#getcomponentjava

Accessing Javascript to C# script

Hey ive been looking at other posts on how to access Javascript code in my other C# files. the Javascript holds the questions and answers, i need to access it in my C# script and say when player answers correctly, move player.

this is the code for c# which moves the player when player roll the dice, but i dont need the dice.

 using UnityEngine;
 using System.Collections;
 
 public class Board : MonoBehaviour
 {
     public GameObject player;
     private int movesRolled;
     private int movesAway;
     private bool diceEnabled;
     private Rect dicePosition;
     
     // Use this for initialization
     void Start () 
     {
         // allow the user to press the Roll Dice button
         diceEnabled = true;    
         dicePosition = new Rect((Screen.width / 2.0f)-35.0f, 10.0f, 70.0f, 30.0f);
     }
     
     // Update is called once per frame
     void Update () 
     {
        if(Input.GetKey(KeyCode.Mouse0))
        {
             Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
             
             RaycastHit hitInfo;
             
             // check the player has clicked on a GameObject
             if (Physics.Raycast(ray, out hitInfo))
             {
                 // check if the clicked object is a board space
                 if (hitInfo.transform.tag.StartsWith("Space"))
                 {
                     // check if we are waiting for the player to move
                     if (diceEnabled == false)
                     {
                         GameObject selectedSquare = hitInfo.collider.gameObject;
                         
                         // make sure the player can only move to a highlighted space
                         if (((SpaceScript)selectedSquare.GetComponent("SpaceScript")).highlight == true)
                         {
                             movePlayer(selectedSquare);
                         }
                     }
                 }
             }
         }    
     }
     
     private int rollDice()
     {
         // turn the Roll Dice button off
         diceEnabled = false;
         
         // get an integer number between 1 and 6 inclusive
         return Random.Range(1,7);
     }    
     
     private void movePlayer(GameObject space)
     {
         // move the player
         ((Player)player.GetComponent("Player")).currentSpace = space;
         player.transform.position = new Vector3(space.transform.position.x, space.transform.position.y + ((Player)player.GetComponent("Player")).yOffset, space.transform.position.z);
         
         // run the rules for the space
         ((SpaceScript)space.GetComponent("SpaceScript")).spaceRules();
         
         // remove the move highlights from the board
         removeHighlightedSpaces();
         
         // reset the rolled dice number to zero
         movesRolled = 0;
         
         // allow the user to press the Roll Dice button
         diceEnabled = true;
     }
     
     private void nextMove()
     {
         movesRolled = rollDice();
         Debug.Log("You have rolled a " + movesRolled);
         movesAway = 0;
         traverseFindMoves(((Player)player.GetComponent("Player")).currentSpace);
         traverseHighlightSpaces(((Player)player.GetComponent("Player")).currentSpace);
     }
     
     private void traverseFindMoves(GameObject boardSpace)
     {
         // get the current traversed space's script
         SpaceScript spaceScript = (SpaceScript)boardSpace.GetComponent("SpaceScript");
         int curMovesAway = spaceScript.movesAway;
 
         //Debug.Log("Checking space " + boardSpace.name + "   current moves away: " + curMovesAway + "   moves away: " + movesAway);
         
         // update the number of moves away the search space is from the player's current space except if the first search space is the current space (in case of any valid loop leading to it)
         if (movesAway < curMovesAway && movesAway != 0)
         {
             Debug.Log("Updated current moves away to " + movesAway);
             spaceScript.movesAway = movesAway;
         }
         
         // make sure we only check the space sequence up to the number of moves away rolled by the dice
         // this prevents infinite recursion if you have a loop in the sequence)
         if (movesAway < movesRolled)
         {
             // check the current traversed space's next spaces (depth first search) and process them
             foreach (GameObject link in spaceScript.next)
             {
                 movesAway = movesAway + 1;
                 traverseFindMoves(link);
                 movesAway = movesAway - 1;
             }
         }
     }
 
     private void traverseHighlightSpaces(GameObject boardSpace)
     {
         // get the current traversed space's script
         SpaceScript spaceScript = (SpaceScript)boardSpace.GetComponent("SpaceScript");
         int movesAway = spaceScript.movesAway;
         
         //Debug.Log("Checking space " + boardSpace.name + "   actual moves away: " + movesAway);
         
         // highlight the space if it is the correct number of moves away from the player's current space
         if (movesAway == movesRolled)
         {
             spaceScript.highlight = true;
             spaceScript.highlightSpace();
         }
 
         // make sure we only check the linked graph structure from the current search space up to the number of moves away rolled by the dice
         // this prevents infinite recursion if you have a loop in the sequence (note first search space may still have a movesAway value of 9999)
         if (movesAway < movesRolled || movesAway == 9999)
         {    
             // check the current traversed space's next spaces (depth first search) and process them
             foreach (GameObject link in spaceScript.next)
             {
                 traverseHighlightSpaces(link);
             }
         }
     }
 
     private void removeHighlightedSpaces()
     {
         SpaceScript spaceScript;
         GameObject [] spaces = GameObject.FindGameObjectsWithTag("Space");
         
         foreach (GameObject boardSpace in spaces)
         {
             // get the space's script
             spaceScript = (SpaceScript)boardSpace.GetComponent("SpaceScript");
 
             // reset the space and update highlight
             spaceScript.reset();
             spaceScript.highlightSpace();
         }
     }
     
     void OnGUI()
     {
         // show Roll Dice button
         if (diceEnabled)
         {
             // when the dice is clicked, process the player's move
             if (GUI.Button(dicePosition,"Roll Dice"))
             {
                 nextMove();
             }
         }
     }
 }

i need to somehow access the javascript code to allow player to move one space if they have answered correctly

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
1

Answer by Jix · Feb 10, 2014 at 11:54 AM

You can access C# script from JS if the C# script in the Plugins folder, but you want the access JS from C#... this is a problem, your only option is using the gameObject.SendMessage() http://docs.unity3d.com/Documentation/ScriptReference/GameObject.SendMessage.html but it can't return any value, but you can modify the method you're calling with SendMessage() to return the value to a c# script (a one inside Plugins folder) then make your C# script (the one that used SendMessage()) to access the C# script that's inside the Plugins folder to get the value returned by the JS script... a long way but you have no other option

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 whydoidoit · Feb 10, 2014 at 01:04 PM 1
Share

Send$$anonymous$$essage can be convinced to return a value if the parameter you pass is a class into which the called function sets it's desired return value.

You access the return value from the class instance you passed as a parameter.

avatar image Jix · Feb 10, 2014 at 06:57 PM 0
Share

That's a better solution

avatar image Mint92 · Feb 10, 2014 at 07:02 PM 0
Share

Cheers for the help, ill give it ago

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

20 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

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Getcomponent unity 4b11 0 Answers

GetComponent not working C# 1 Answer

C# GetComponent Issue 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