- Home /
Accuracy doesn't work. No enemies showing up.
Hello,
I am totally new to JavaScript and Unity. I'm making a simple game, watching step by step Tutorial of making it. I'm stuck at one problem. What I want to do, is to make accuracy and if it's more than 60%, then next level and enemies are not showing up. But thing is, when I start game, no enemies aren't showing up, at the beginning of game. Game is like a whack-a-mole type game, where need to punch. Here is code, how in Tutorial guy is writing and all is working:
#pragma strict
static var enemySpeed : float = 2;
static var enemyJumpSpeed : float = 2;
static var totalEnemies : int;
static var wave : int = -1;
var enemiesToSpawn : int;
var enemy : GameObject;
private var gameOver : boolean;
static var accuracy : float;
static var punchesHit : float;
static var punchesThrown : float;
function Update () {
if (!gameOver) {
accuracy = (punchesHit / punchesThrown) * 100;
if (totalEnemies == 0) {
LevelEndSequence();
}
}
}
function LevelEndSequence() {
enemiesToSpawn += 1;
wave += 1;
totalEnemies = enemiesToSpawn;
if (accuracy < 60) {
gameOver = true;
} else {
yield WaitForSeconds(2);
for (var a = 0; a < enemiesToSpawn; a++) {
Instantiate(enemy, Vector3(Random.Range(3750,3755),-1.5,Random.Range(-2267,-2264) ), transform.rotation);
}
}
punchesHit = 0;
punchesThrown = 0;
}
he has 2.5 Unity, I have 3.5 and nothing working for me. Game simulates, but no enemies at the begining are showing up.
If I don't add:
if (accuracy < 60) {
gameOver = true;
} else {
, then, all work, but, of course, new enemies aren't showing up. and no accuracy.
Thanks, alucardj for comment. Maybe you can give any solution for this? I attached picture, where you can see where I assign how many enemies to spawn, when I enter any amount in ''Enemies To Spawn'' field, they will change to +1, when game simulates.
Alucardj, full tutorial can't be seen, I bought it, but I uploaded video, in where he does accuracy. I'm stuck in the midlle of video, where he writes code about accuracy. All is working for him, but not for me. Did all, like him before, till that video and part and all worked out, but not the accuracy. I uploaded video here: http://www9.zippyshare.com/v/56558009/file.html
Please let me know, if you need any files or etc., from me, to get accuracy working. At the moment I'm stuck and can't get work going and need this accuracy, to get end game function. I hope this video will help you to fix my problem.
Thanks, all, I really appreciate !!! Will try it. Thanks !!
I cannot see where any variables are assigned. Therefore, as soon as you play, Update checks and sees that totalEnemies is 0, then LevelEndSequence(). Now from the Update, accuracy was deter$$anonymous$$ed also as zero (( 0 / 0) * 100), so LevelEndSequence immediatly sees accuracy as zero, and ends the game. All in the first frame. Even if you change the variables in the inspector, you should asign some value when you initialize the variables.
Answer by haico1992 · Jan 06, 2013 at 02:54 PM
function LevelEndSequence() {
enemiesToSpawn += 1;
wave += 1;
totalEnemies = enemiesToSpawn;
if (accuracy<1){
accuracy=100}
else
if (accuracy < 60) {
gameOver = true;
} else {
yield WaitForSeconds(2);
for (var a = 0; a < enemiesToSpawn; a++) {
Instantiate(enemy, Vector3(Random.Range(3750,3755),-1.5,Random.Range(-2267,-2264) ), transform.rotation);
}
}
punchesHit = 0;
punchesThrown = 0;
}
I pretty sure it all because of your accuracy = 0 at the start. Add 2 line above "if (accuracy < 60) " should solve it.
On initialization, the vars punchesHit and punchesThrown are unassigned, so the equation accuracy = (punchesHit / punchesThrown) 100; gives the result NAN*, therefore if (accuracy<1){accuracy=100;} will not fix the problem. There are other scripts in the project that initialize these variables (as well as other vars like enemiesToSpawn), and it seems the problem lies there. I have proven this with my script and package in my comment. The above script actually works, the problem is in the initialization of the static variables on this script.
Your answer
Follow this Question
Related Questions
How to display previous players scores as well as current player score at the end of the game? 0 Answers
Sending a string via email from Unity? 2 Answers
[Solved] Generating Particles with Attack Animation 1 Answer
How to create dynamic array of type GameObject using javascript? 2 Answers
accessing a variable from one script in another with Unity 1 Answer