- Home /
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
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
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.
Your answer
![](https://koobas.hobune.stream/wayback/20220613133058im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
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