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 rottweiler · Jan 24, 2013 at 04:36 PM · c#javascriptvariable

Problem with translating C# variable to JS.

Hello.

I am putting this code in Standard Assets folder:

 using UnityEngine;
 using System;
 using System.Collections;
 using System.IO;    

 public class ResolutionChanger : MonoBehaviour
 {
         public string fileName = "Settings.ini";
     public float timer;     
   private int screenWidth;
     private int screenHeight;   
     private int fullscreen;    

     void Start()
     {      
         using (StreamReader fileStream = File.OpenText(Path.GetFullPath(fileName)))
         {          
             string fileLine = "";
             while ((fileLine = fileStream.ReadLine()) != null)
             {
                 string option = fileLine.Substring(0, fileLine.IndexOf("="));
                 switch (option)
                 {
                     case "gametime":
                         timer = Convert.ToInt32(fileLine.Substring(fileLine.IndexOf("=") + 1));
                         break;
                     case "screenwidth":
                         screenWidth = Convert.ToInt32(fileLine.Substring(fileLine.IndexOf("=") + 1));
                         break;
 
                     case "screenheight":
                         screenHeight = Convert.ToInt32(fileLine.Substring(fileLine.IndexOf("=") + 1));
                         break;
 
                     case "radius":
                         fullscreen = Convert.ToInt32(fileLine.Substring(fileLine.IndexOf("=") + 1));
                         break;                    
                     }
  
             }           
             fileStream.Close();
         }        
                Screen.SetResolution(screenWidth, screenHeight, false);
     }     

 }

And its working. I have access from text file. But when i try to use the variable "timer" in other JS file, like this:

 private var csScript : ResolutionChanger; 
    
 function Awake()  
 {  
    csScript = this.GetComponent("ResolutionChanger");
 }
 
 var guiScore : GUIText;    
 var myTimer = csScript.timer; 
 
 function Update (){    
         myTimer -= Time.deltaTime;            
     if(myTimer < 0){
         Application.LoadLevel("GameOver");
         }            
 var count : int = myTimer;        
 guiScore.text = " " + count;  
 }

I have an error: NullReferenceException: Object reference not set to an instance of an object Timer..ctor () (at Assets/scripts/Timer.js:10) Whats the problem?

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 sparkzbarca · Jan 24, 2013 at 04:45 PM

you cant mix and match java and C# like that. Google accessing a javascript from a C# unity for more information.

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 rottweiler · Jan 24, 2013 at 05:43 PM 0
Share

But http://www.41post.com/1935/program$$anonymous$$g/unity3d-js-cs-or-cs-js-access says that i can do this

avatar image
0

Answer by Loius · Jan 24, 2013 at 06:13 PM

csScript isn't set to any thing.

Order of operations: First executed is code outside any function, then Awake, then Start, then Update.

You're setting something = csScript.something outside a function, then you're setting csScript = something in Awake.

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

11 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

Related Questions

Need to use 2 different language scripts. 1 Answer

What is this C# code in javascript 0 Answers

Javascript not being updated, variables being overridden, but C# is fine 1 Answer

How to create read-only variables 3 Answers

Multiple Cars not working 1 Answer


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