Game progress from c# to Unityscript.

Hi all,

i got this C# code and i am having trouble converting it to UnityScript.

C#

 // puzzle progress - percentage 0-100
    protected float puzzleProgress     
    {
        get
        {
            return (piecesPlaced/pieceCount) * 100;
        }
    }

Unityscript.

    function puzzleProgress() : float
    {
         return (piecesPlaced/pieceCount) * 100;
    }

So the code calls on two functions to check the progress of the game, piecesPlaced (how many of the pieces from the starting amount have been placed in their right spot.)

And pieceCount (how many pieces have not been placed yet and still remain in play)

With the code i have up there for Unityscript i get the following error.

BCE0051: Operator ‘/’ cannot be used with a left hand side of type ‘function(): int’ and a right hand side of type ‘function(): int’.

Anyone got any ideas how to solve this?

Any help would be appreciated. :slight_smile:

typecast the results from the functions as float, for example (this is C# code, look for js code on the webz): float result = (float)(function1())/(float)(function2()) * 100; function1 and function2 return int.

okay, thanks ill have a look for a js example. :slight_smile:

I’d switch to c# :wink:

the problem is that

piecesPlaced/pieceCount is meant to be piecesPlaced()/pieceCount() cause the error implies that this are functions and functions need () to return anything

the compiler would never warn you that int / int will yield in a crap result cause it devides without any fractions but modulo reminder

I would strongly recommend to follow basic .NET naming schemes, that saves you such headaches, which your AS / JS naming scheme definitely doesn’t

  1. Properties always tart with upercase and after that camel
  2. Functions always start with uppercase and after that came
  3. local variables start lowerCase and after that camel
  4. class variables / instance variables either lowercase and then camel or underline lowercase camel ie _myVariable

that way you will never mix variable and function at all again

also the code is not hte C# equivalent. the equivalent would be

function [b]get[/b] puzzleProgress() : float
    {
         return (piecesPlaced/pieceCount) * 100;
    }

cause what you have there is just a function, not a property (“getter”) at all, tahts why not having the parenthesis does not work

Yes they both are functions piecesPlaced() : int and pieceCount() : int

I am aware functions start with uppercase and then camel e.g. function OnGUI()

However the scripts that i am trying to convert do not follow this rule and to avoid confusion when cross checking code i have decided to leave it this way.

Yes i forgot to put in the get, thanks for reminding me. :slight_smile:

I am still looking through typecasting to try and solve this error, but am having trouble finding some examples that i can understand fully, can anyone point me in a direction to some basic typecasting examples.

Thanks.