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 smweis1 · May 07, 2014 at 06:02 PM · javascriptmac

Custom Script issue from PC to Mac (NOT IOS)

Hi all,

I'm trying to build a for a Mac that will allow me to collect some data on each mouse click. The script I have (JavaScript) exports the value of a vector and writes it to a text file.

This all works fine on a Windows 7 (which is also the OS I'm using for the Unity Editor), but when I try to build it for a Mac, I get a NullReferenceException for an object that I'm trying to change. (It's a GUIText Object that calls a random element from a list, the name of a building, and displays it). The default GuiText is all that displays when I run it on a Mac. Everything else works fine, so I'm guessing it's just an issue with the script.

I don't have a Unity Editor for the Mac, is there any way to debug it in Windows? Also, I'm pretty sure I DON'T need to use #pragma strict, as this is a standalone build, not an iOS build. But someone correct me if I'm wrong?

I'm happy to post the full code if that'll help.

EDIT:

It appears like it might be a #pragma strict problem after all. I think this is the line of code I need to alter:

 GetComponent(CharacterController).transform.position = diamonds[startLandmarkIndex].transform.position;

diamonds, startLandmarkIndex are both declared (Array and int, respectively). And it's throwing a BCE0019: 'transform' is not a member of 'Object'.

The rest of the code:

 #pragma strict
 
 import System;
 import System.IO;
 
 var diamonds = List.<GameObject>();
 var names = List.<String>();
 var pointingDiamondIndex : int;
 var facingDiamondIndex : int;
 var targetBuildingIndex : int;
 var targetBuildingIndicesRemaining : Array;
 var targetBuildingIndicesRemainingIndex : int;
 var pointingPromptObject : GameObject;
 var waitingForClick : String;
 var mainCamera : Camera;
 var screenRay : Ray;
 var currentPosition : Vector3;
 var facingDiamondPosition : Vector3;
 var pointingAngle : float;
 static var dataDirectory : DirectoryInfo;
 
 
 function Start() {
 
     //load file-location
     dataDirectory = new DirectoryInfo("C:");
     WriteFile("out.txt","pointingDiamondIndex facingDiamondIndex targetBuildingIndex pointingAngle\n");
     pointingPromptObject = GameObject.Find("PointingPrompt");
     mainCamera = Camera.main;
 
     // populate arrays
     diamonds.Add(GameObject.Find("Batty diamond"));
     names.Add("Batty House");
     diamonds.Add(GameObject.Find("Lynch diamond"));
     names.Add("Lynch Station");
     diamonds.Add(GameObject.Find("Harvey diamond"));
     names.Add("Harvey House");
     diamonds.Add(GameObject.Find("Golledge diamond"));
     names.Add("Golledge Hall");
     diamonds.Add(GameObject.Find("Sauer diamond"));
     names.Add("Sauer Center");
     diamonds.Add(GameObject.Find("Tobler diamond"));
     names.Add("Tobler Museum");
     
     startPointingSet(0);
 }
 
 function startPointingSet(startLandmarkIndex:int) {
     if (startLandmarkIndex <= 5) {
         pointingDiamondIndex = startLandmarkIndex;
         var cc : CharacterController = GetComponent(CharacterController) as CharacterController;
         // move to diamond
         GetComponent(CharacterController).transform.position = diamonds[startLandmarkIndex].transform.position;
         
         if (startLandmarkIndex == 0) facingDiamondIndex = 1;
         else if (startLandmarkIndex == 1) facingDiamondIndex = 2;
         else if (startLandmarkIndex == 2) facingDiamondIndex = 1;
         else if (startLandmarkIndex == 3) facingDiamondIndex = 4;
         else if (startLandmarkIndex == 4) facingDiamondIndex = 5;
         else if (startLandmarkIndex == 5) facingDiamondIndex = 4;
 
         
         targetBuildingIndicesRemaining = [0,1,2,3,4,5];
         targetBuildingIndicesRemaining.RemoveAt(startLandmarkIndex);
         Debug.Log("startPointingSet() targetBuildingIndicesRemaining: " + targetBuildingIndicesRemaining.join(","));
         targetBuildingIndicesRemaining = shuffle(targetBuildingIndicesRemaining);
         
         showPointingQuestion();
     }
     else {
         // all done -- return to browser
         Debug.Log("all done -- return to browser");
         Application.ExternalCall("doneWithPointing");
     }
 }
 
 function showPointingQuestion() {
     if (targetBuildingIndicesRemaining.length > 0) {
         targetBuildingIndex = targetBuildingIndicesRemaining[0];
         // show instructions
         pointingPromptObject.guiText.text = "Point to " + names[targetBuildingIndex];    
     }
     else {
         startPointingSet(pointingDiamondIndex + 1);
     }
 }
 
 function Update() {
     screenRay = mainCamera.ScreenPointToRay(Vector3((mainCamera.pixelWidth / 2), (mainCamera.pixelHeight / 2), 0));
     Debug.DrawRay(screenRay.origin, screenRay.direction * 2000, Color.red);
     Debug.DrawLine(screenRay.origin, diamonds[facingDiamondIndex].transform.position, Color.blue);
         if (Input.GetKey ("escape")) {
         Application.Quit();
     }
     
 }
 
 function OnGUI() {
     if (Input.GetButton ("Fire1")) {
             if(targetBuildingIndicesRemaining.Count>0){
                 targetBuildingIndicesRemaining.RemoveAt(0);
                 Debug.Log("OnGUI() targetBuildingIndicesRemaining: " + targetBuildingIndicesRemaining.join(","));
                         
                 screenRay = mainCamera.ScreenPointToRay(Vector3((mainCamera.pixelWidth / 2), (mainCamera.pixelHeight / 2), 0));
                 currentPosition = GetComponent(CharacterController).transform.position;
                 facingDiamondPosition = diamonds[facingDiamondIndex].transform.position;
                 pointingAngle = Vector3.Angle((facingDiamondPosition-currentPosition), screenRay.direction);
                 Debug.Log("pointing angle: " + pointingAngle);
                 
                 // send pointing angle to the browser
                 Application.ExternalCall("recordPointingQuestion", pointingDiamondIndex, facingDiamondIndex, targetBuildingIndex, pointingAngle);
                 
                 
                 WriteFile("out.txt",pointingDiamondIndex+" "+ facingDiamondIndex+" "+targetBuildingIndex+" "+pointingAngle+"\n");
                 
                 
                 
                 Input.ResetInputAxes(); // we don't want this repeated multiple times
     
                 showPointingQuestion();
             } else{
                 Application.Quit();
             }
     }
 }
 
 //new function for saving into file on disk
 static public function WriteFile(fileName : String, data : String) {
     var writer : StreamWriter;
     var t : FileInfo = new FileInfo(dataDirectory.FullName + '/' + fileName);
     writer = t.AppendText(); 
     writer.Write(data); 
     writer.Close();    
 }
 
 function shuffle(array : Array) {
     var temp : int;
     
     for (var i : int = 0; i < array.length; i++) {
         temp = array[i];
         var swapIndex :  int = UnityEngine.Random.Range(0, array.length - 1);
         array[i] = array[swapIndex];
         array[swapIndex] = temp;
     }
     return array;
 }
  
Comment
Add comment · Show 2
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 rutter · May 07, 2014 at 07:58 PM 0
Share

What is diamonds? Is it an Array()? If so, you're relying on dynamic typing every time you pull an object out of it, because Array() does not save any type information.

Dynamic typing is potentially powerful, but has a performance penalty and isn't supported on all Unity platforms. It's the most important feature that's turned off by using #pragma strict.

You've set up a scenario where the interpreter isn't able to infer a specific type for your diamond, so it's assu$$anonymous$$g it's a basic Object (because it's the safest guess). Since the class Object doesn't have a field named transform, you're getting that error.

You could try an explicit cast:

 //NOTE: assumes 'diamond' is a GameObject!
 var diamond  = diamonds[startLandmarkIndex] as GameObject;
 GetComponent(CharacterController).transform.position = diamond.transform.position;
avatar image smweis1 · May 09, 2014 at 08:49 PM 0
Share

See below - this has fixed the diamonds and characterController issues, but I'm still having trouble with targetBuildingIndicesRemaining. I can't get it to do both things - be a list of integers (declared as integers), and allow methods like join, add, removeat, etc.

Any further suggestions on that front?

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Graham-Dunnett · May 07, 2014 at 09:21 PM

 // javascript
 var cc : CharacterController = GetComponent(CharacterController) as CharacterController;
 cc.transform.position = diamond.transform.position;
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 smweis1 · May 09, 2014 at 04:08 PM 0
Share

This comment seems to have fixed my pragma strict issue, but I am having issues now with an Obj downcasting to int. I'm not sure how to amend the script to change Array methods in javascript to list methods that function the same way. I've posted the entire code, to hopefully get some help.

Errors pop up on lines 140, and 79.

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

22 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 avatar image avatar image

Related Questions

Setting Scroll View Width GUILayout 1 Answer

JDK home issue (Mac to android) 0 Answers

Can someone help me fix my Javascript for Flickering Light? 6 Answers

Text To Speech? 0 Answers

Editor other than UniTron for Unity Mac 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