- Home /
How Do I Update a cSharp script var from js?
I've been looking into this a bit and still havn't got it to work. I'd be willing to either get them working together somehow, or use a shared global variable, or rewrite the csharp script to js (although it extends another csharp script i didnt write). I moved the csharp script to Standard Assets which seemed to help, but I still get an error: NullReferenceException: Object reference not set to an instance of an object BallControl.Update () (at Assets/Game/BallControl.js:37)
I'm using the following cSharp script to make a GUI for my game:
using UnityEngine;
using System.Collections;
public class GameGui : VRGUI
{
public int lives = 5;
public int score = 0;
public override void OnVRGUI()
{
GUI.skin.label.fontSize = 30;
GUILayout.BeginArea(new Rect(300, 100, 200, 100));
GUILayout.Label("Lives: " + lives);
GUILayout.EndArea();
GUILayout.BeginArea(new Rect(900, 100, 200, 100));
GUILayout.Label("Score: " + score);
GUILayout.EndArea();
}
public void RemoveLife() {
lives = lives - 1;
}
}
This is the JS script i am trying to change the cSharp one from:
#pragma strict
var left = -100;
var right = 100;
var top = 100;
var bottom = -100;
var xV : float;
var yV : float;
var zV : float;
var damage = 100;
var guiscript : GameGui;
function Start () {
xV = Random.Range(0,0.5);
yV = Random.Range(0,0.5);
zV = Random.Range(0,2.0);
}
function Update () {
//move the ball
transform.position.x = transform.position.x + xV;
transform.position.y = transform.position.y + yV;
transform.position.z = transform.position.z + zV;
//ball bounces
if(transform.position.x < left || transform.position.x > right) xV = -1 * xV;
if(transform.position.y < left || transform.position.y > right) yV = -1 * yV;
if(transform.position.z > right) zV = -1 * zV;
//lose a life
if(transform.position.z < left) {
transform.position = Vector3(0,0,0);
xV = Random.Range(0,0.5);
yV = Random.Range(0,0.5);
zV = Random.Range(0,2.0);
guiscript = this.GetComponent("GameGui");
guiscript.RemoveLife();
}
}
I am just trying to change the GUI in csharp when stuff happens in js. Any help would be much appreciated!
Answer by DaveA · Nov 01, 2013 at 08:21 AM
guiscript.removeLife();
I don't see this routine in GameGui.cs. You need that.
I had it there and it still didnt work? What is the right way to declare it? I am not familiar with csharp.
Answer by LeapGamer · Nov 01, 2013 at 11:36 AM
I figured it out. I didn't have the two scripts on the same GameObject, so I had to find that GameObject first.
var GameGui : GameObject;
var GuiScript : GameGui;
/* and later */
GameGui = GameObject.Find("OVRCameraController");
GuiScript = GameGui.GetComponent("GameGui");
GuiScript.RemoveLife();
Your answer
Follow this Question
Related Questions
Accessing a variable from js with c# script 1 Answer
Access static variable c# 1 Answer
call function in javascript from c# script 1 Answer
Passing data trough different scrips 2 Answers