Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by renalds · Jan 05, 2013 at 06:15 PM · javascript

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.

alt text

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 !!

untitled.jpg (186.7 kB)
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jan 06, 2013 at 05:19 AM 0
Share

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.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image AlucardJay · Jan 06, 2013 at 03:09 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges