Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
This question was closed Aug 25, 2016 at 02:35 AM by dancehound for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by dancehound · Aug 24, 2016 at 10:15 PM · c#variablesfloat

One script says the variable is 0 but the other says -37

Ok, so this is the first time I am going to be asking a question but before I do it I would like you guys to know that I started coding in c#+unity a few days ago. So sorry if my code is a bit sloppy. I am using this tutorial series by Brackley on a tower defense game but I stopped watching and continued to code the rest of it myself. Currently, I have a WaveSpawner script setting numOfEnemies and numOfEnemies2 to 0. When I logged it was always correct going up by 1, 2 and etc. But in the other script Enemy.cs I got the script and I am trying to make the numOfEnemy and numOfEnemy2 go down by one. But instead, when I debug it, it comes out as -37 for both. But when I log both of the values in the two different scripts at the same time the wavespawner script still has the correct number 0, 1, 2 etc and the enemy has -37, -36. Thank you for the help!

Enemy.cs

 using UnityEngine;
 using System.Collections;
 
 public class Enemy : MonoBehaviour {
 
     [Header("Attributes")]
     public float enemyHealth;
     public GameObject deathEffect;
 
     public GameObject gameCore;
 
     private string enemy1 =  "Enemy"; 
     private string enemy2 =  "Enemy2";
 
 
 
     void Update()
     {
 
         WaveSpawner gameCoreScript = (WaveSpawner)gameCore.GetComponent("WaveSpawner");
 
         GameObject currentEnemy = gameObject;
 
         string currentEnemyTag = currentEnemy.tag;
 
 
         if (enemyHealth <= 0) {
 
             if (currentEnemyTag == enemy2) {
 
                 //Debug.Log ("Enemy2 Killed!");
                 //Debug.Log (gameCoreScript.numOfEnemy2 + "Enemy2 remain!");
                 //gameCoreScript.numOfEnemy2 -= 1;
 
             } else if (currentEnemyTag == enemy1) {
                 Debug.Log (gameCoreScript.numOfEnemy);
                 //Debug.Log("Enemy Killed!");
                 //Debug.Log (gameCoreScript.numOfEnemy + "Enemy remain!");
                 //gameCoreScript.numOfEnemy -= 1;
 
             }
 
 
             GameObject effectHealthIns = (GameObject)Instantiate (deathEffect, transform.position, transform.rotation);
             Destroy (effectHealthIns, 2f);
             Destroy (gameObject);
         }
     }
 
 }
 
 

WaveSpawner.cs

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class WaveSpawner : MonoBehaviour {
 
     [Header("Game Mechanics")]
     public Transform enemyPrefab;
     public Transform enemy2Prefab;
     public Transform spawnPoint;
     public Text waveCountdownText;
     public Text remainingEnemiesText;
 
     [Header("Game Attributes")]
     public float timeBetweenSpawns = 0.5f;
     public float timeBetweenWaves = 5f;
 
 
     private float countdown = 2.5f;
     private float waveNumber = 0;
 
     public int waveIndex = 0;
     public float numOfEnemy = 0;
     public float numOfEnemy2 = 0;
 
 
 
 
     void Update()
     {
         //Debug.Log (numOfEnemy);
         //Debug.Log (numOfEnemy2);
         if (countdown <= 0f) 
         {
             StartCoroutine(SpawnWave());
             countdown = timeBetweenWaves;
             waveNumber = waveNumber + 1;
         }
             
 
         countdown -= Time.deltaTime;
 
         float enemiesLeft = numOfEnemy + numOfEnemy2;
         waveCountdownText.text = Mathf.Round(waveNumber).ToString();
         remainingEnemiesText.text = Mathf.Round(enemiesLeft).ToString();
     }
 
     IEnumerator SpawnWave ()
     {
         waveIndex++;
         Debug.Log ("Wave Coming!");
 
         for (int i = 0; i < waveIndex; i++) {
                 numOfEnemy = numOfEnemy + 1;
                 SpawnEnemy ();
                 yield return new WaitForSeconds (timeBetweenSpawns);
             if (numOfEnemy > waveNumber  && numOfEnemy2 < waveNumber) 
                 {
                 numOfEnemy2 = numOfEnemy2 + 1;
                     SpawnEnemy2 ();
                 yield return new WaitForSeconds(timeBetweenSpawns);
                 }
             }
 
             }
 
         
 
 
 
 
 
     void SpawnEnemy()
     {
             Instantiate (enemyPrefab, spawnPoint.position, spawnPoint.rotation);
     }
 
 
 
     void SpawnEnemy2()
     {
         Instantiate (enemy2Prefab, spawnPoint.position, spawnPoint.rotation);
 
 }
 
 }
Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

Answer by dancehound · Aug 25, 2016 at 02:35 AM

Turns out I just had to restart my unity, so answered

Comment
Add comment · 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

Follow this Question

Answers Answers and Comments

224 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Double vs Float 1 Answer

C# Unity dot syntax exercise correct solution? 1 Answer

Timer float not evaluating correctly when I check if it's less than another number 3 Answers

Can someone explain this to me? Player myPlayer = new Player(); 1 Answer

Can I compare a Transform variable (x, y, z) against float variables? 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