How can I convert this Algebra to code that unity will understand?

Ok so, I have the source code (See below) and I am trying to make a coins function inside of my score class. and every time my score reaches an interval of 500 i want the coins to go up by one. I have a formula however unity replies with an error, The formula is (currentScore / 500 = currentCoins), and the error is (Assets/Score.js(24,28): BCE0049: Expression ‘currentScore / 500’ cannot be assigned to. How can i change this code so that my lives will work how i want it too.

static var currentScore : int = 0;
static var highScore : int;
var isPaused : boolean = false;
var currentCoins : int;
var coinValue : int = 1;
	
function Start ()
{
	currentScore = 0;
	
}
function OnGUI ()
{
	GUI.Label (new Rect(5, 10, 100, 200), "Score: " + currentScore);
	GUI.Label (new Rect(5, 25, 100, 200), "High Score:" + highScore);
	GUI.Label (new Rect(475, 10, 200, 200),"Current Version: BETA 0.0.7");
	GUI.Label (new Rect(5, 40, 100, 200), "Coins:" + currentCoins);
	
}
function Update()
{

    currentScore / 500 = currentCoins;

	if(currentScore > highScore)
	{
	highScore = currentScore;
	}
	
	if(Input.GetButtonDown ("Pause"))
	{
		Pause();
	}if(currentScore)
	{
		currentCoins+coinValue;
	}
}

What is this line purpose : if(currentScore) { currentCoins+coinValue; }

Thank You everyone soooooo much, it works just the way I want it to now, but I couldn't have down it without everyones help, it turned out to be a simple mistake although after reading through everyones responses I took a little bit from each and combined them into a working system. :-) Thank You again! -Zedock-

just ignore that, that was going to be an if statement however im not using it anymore

3 Answers

3

please fix your code formatting

this line:

currentScore / 500 = currentCoins;

should read:

currentCoins = currentScore / 500;

this line will cause a problem too:

currentCoins+coinValue;

what are you trying to do here?

i didn't make it an answer... but feel free to blame me. your suggestion will simply divide the score by 500, not set the coin value to score/500.

@Benproductions1: It was a comment and i converted it to an answer ^^. "currentScore /= 500" doesn't make sense since it would change the score and not set the coins. Yes this "question" isn't really an answerable question but more a scream for education in fundamental programming and gjf has said what was to be said.

Just going off the variable names, maybe: currentCoins = coinValue * currentScore / 500

@Bunny83, just out of curiosity, how do you normally treat questions like this, where they are "more a scream for education in fundamental programming". Do you think we should link them to a tutorial, answer the question directly, or try to teach them ourselves?

if ( (currentScore % 500) == 0 )
{
currentCoins = currentScore / 500;
}
Each time the modulo operator (“%”) gives you a “rest” of 0, the coins are increased. If I understood your question correctly this should work.

The modulo check is dangerous. What is score isn't increasing by 1 but by 3, 7 or 101. You wouldn't hit your condition. Since the modulo is also a divide operation (ok, not as heavy as a normal divide but equally neglectable) you don't save any performance here. Just assign it every frame. It's just a simple integer divide. Keep in mind that it's more important that your program does it's task right instead of fast but wrong. A case of [premature optimisation][1] [1]: http://en.wikipedia.org/wiki/Program_optimization#When_to_optimize

You're right ... but because of "reaches an interval of 500" I thought that the score is increased one by one or at least so that it reaches exactly values of 500, 1000, 1500, etc. and that the question is: How can you achieve that the coin-number changes exactly whenever a 500er step is reached.

You could tweak the modulo method to keep the last result of the modulo, then subtract it from the current check. The result should be the number of units of 500 since last check.

On modern CPUs, + - / * are all the same speed. Modulo is very slightly slower. So the advice about avoiding premature optimization is even more relevant since some traditional "optimizations" such as avoiding division no longer even apply.

Like this

var coinStep : int = 0;

function Update()
{
 	var newCoinStep : int = currentScore / 500;
    while (newCoinStep-- > coinStep)
    {
    	++currentCoins;
    }
}

So, keep track of how many 500’s you had last time and if you now have more of 500’s, add coins.
This also handles situations where you gain i.e. 1000 points and need to get 2 coins for it.

Like mentioned by others above, you should create a method for increasing currentScore, because this check only needs to happen whenever score is increased… not every update. It’s not a problem in a simple case like this but it’s just a good practice to learn