- Home /
Multiply float by int?
So I'm trying to create a score system that goes up by 100 every second. I get this error: "Only assignment, call, increment, decrement, and new object expressions can be used as a statement"
This is my code so far:
using UnityEngine;
using System.Collections;
public class ScoreperSec : MonoBehaviour {
public int startingScore = 0;
public int currentScore = 0;
public GUIText scoregui;
private float time;
private int score;
void Update (){
time+=Time.deltaTime;
//currentScore += (int)time;
print ((int)time);
(int)time * 100 == score;
scoregui.text = "Score: "+ score;
}
}
(int)time 100 == score; may be error, i think you should like this if ((int)time 100 == score)
Answer by Kiwasi · Jan 23, 2015 at 03:14 AM
score = (int)time * 100;
You probably also need
scoregui.text = "Score: "+ score.ToString();
Shouldn't you use score = (int)(time * 100) otherwise your dropping the decimal values of time and multiplying that by 100?
As written it will increase by 100 points at the end of every second.
If you want it to go from 0 to 100 over one second then your code should be used.
The OP was not overly clear on which effect he was after.
Answer by RalphTrickey · Jan 23, 2015 at 02:58 AM
Is it this statement?
(int)time * 100 == score;
== is a comparison and doesn't make any sense in this context. If you meant = then the left hand side is not a variable (lvalue).
I believe that is the issue, yeah. I'm not sure how I would fix it though.
score = (int)(time * 100); ? I believe this is what you are trying to do.
Your answer
Follow this Question
Related Questions
Adding coin score to multiplied value 2 Answers
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Error when checking if float is a whole number 2 Answers
Convert Text to float 3 Answers