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 LeapGamer · Nov 01, 2013 at 07:45 AM · c#access

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!

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
0

Answer by DaveA · Nov 01, 2013 at 08:21 AM

guiscript.removeLife();

I don't see this routine in GameGui.cs. You need that.

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 LeapGamer · Nov 01, 2013 at 09:43 AM 0
Share

I had it there and it still didnt work? What is the right way to declare it? I am not familiar with csharp.

avatar image
0

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();
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

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

16 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

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

Getcomponent through tag and findgameobject with tag and do a function but somehow gives errors (It's hard to explain) 1 Answer

Passing data trough different scrips 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