- Home /
weird result when substracting/adding;
I have a problem....I have a very simple script that should substract or add one variable from another every update but it always substracts only once:
#pragma strict
var num : int;
var dmg : int;
function Start () {
}
function Update ()
{
num = 5;
dmg = 1;
num = num - dmg;
print(num);
}
I simply substrack two integers one from another snd print the result in the console....in this case it shoud give me 4, 3, 2, and so on but it always stops at 4 and if I try to add the two integers it stops at 6.
Answer by Dave-Carlile · Jun 21, 2013 at 04:11 PM
The Update
function is called every frame. Inside that function you're setting num
to 5 and dmg
to 1, then subtracting. So each time through the code - i.e. every time Update
is called - you'll see 4
in the log.
You need to initialize your variables in the Start
function. Move your num = 5
and dmg = 1
code to Start
.
Your answer
Follow this Question
Related Questions
Adding and Substracting ammunition 1 Answer
Conditional operator problem S: 1 Answer
Problem at add Score+1 When Pass rocks 0 Answers
Can you determine the speed of falling? 3 Answers
Make car game to multiplayer 1 Answer