The question is answered, right answer was accepted
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);
}
}
Answer by dancehound · Aug 25, 2016 at 02:35 AM
Turns out I just had to restart my unity, so answered
Follow this Question
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