Search code examples
unity-game-engineunityscript

Unity Coin Collection script won't compile correctly


Okay so I have a script being applied to an empty game object to display the score.

CoinController.js

#pragma strict
static var coinCount;

function OnGUI() {
var coinText = "Total Coins: " + coinCount;
GUI.Box (Rect(Screen.width -150,20,130,20), coinText);
}

and a script that is applied to the coin objects which should increment the score when the player collides with it and destroy the gameobject.

CoinBehavior.js

#pragma strict

function OnTriggerEnter(other : Collider){
   switch (Collider.gameObject.name){
     case "Character":
        CoinController.coinCount++;
        Destroy(this.gameObject);
        break;
     case "Character(Clone)":
        CoinController.coinCount++;
        Destroy(this.gameObject);
        break;
   }
}

I can't for the life of me get this code to work. I have the script working in c# but am trying to start using java. I know there are big syntactical differences and I'm having trouble figuring out how to make the errors go away.


Solution

  • static var coinCount;
    

    In Javascript this will be refered as an Object.

    CoinController.coinCount++;
    

    You cannot use the ++ operator in on a Object if you have declared

    #pragma strict
    

    Your solution is tell compiler that coinCount is a integer.

    static var coinCount:int;
    

    or simply remove #pragma strict and you will not be enforced on your data types( which i personally don't recommend).