Cant add to int from another script?
Hello everybody.
I want to add 10 health to my enemy every wave. In the SpawnWave() section of my script I added:
GameObject.Find("EnemyTest").GetComponent<EnemyTest>().maxHealth + (10);
GameObject.Find("EnemyTest").GetComponent<EnemyTest>().health + (10);
These lines returns the error
"WaveSpawner.cs(137,88): error CS0201: Only assignment, call, increment, decrement, and new object expressions can be used as a statement".
Can anybody tell me what this means and how to fix it? :)
Answer by JEFFDOG11111 · Sep 07, 2016 at 08:22 PM
//Try this :
//----[ShortHand]----///
GameObject.Find("EnemyTest").GetComponent().maxHealth += 10;
GameObject.Find("EnemyTest").GetComponent().health += 10;
or
/
//----[FullHand]----//
GameObject.Find("EnemyTest").GetComponent().maxHealth = GameObject.Find("EnemyTest").GetComponent().maxHealth + 10;
GameObject.Find("EnemyTest").GetComponent().health = GameObject.Find("EnemyTest").GetComponent().health + 10;
Arrggg.. this is just giving me another error and now enemies dot spawn anymore :( Here is the whole SpawnWave() function:
[code]
IEnumerator SpawnWave ()
{
for (int i = 0; i < waveIndex; i++)
{
}
_shop.GetComponent<$$anonymous$$oney$$anonymous$$anager> ().add$$anonymous$$oney (50);
Debug.Log ("Wave Inco$$anonymous$$g!");
GameObject.Find("EnemyTest").GetComponent<EnemyTest>().maxHealth = GameObject.Find("EnemyTest").GetComponent<EnemyTest>().maxHealth + 10;
GameObject.Find("EnemyTest").GetComponent<EnemyTest>().health = GameObject.Find("EnemyTest").GetComponent<EnemyTest>().health + 10;
waveIndex++;
for (int i=0;i<normalEnemyNumber[waveIndex];i++)
{
spawnEnemy();
yield return new WaitForSeconds(1f);
}
for (int i=0;i<bossEnemyNumber[waveIndex];i++)
{
spawnBoss();
yield return new WaitForSeconds(5f);
}
}
[/code]
This is now giving me an error on the line I just added.
NullReferenceException: Object reference not set to an instance of an object WaveSpawner+c__Iterator3A.$$anonymous$$oveNext () (at Assets/Scripts/WaveSpawner.cs:137) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine(IEnumerator) WaveSpawner:Update() (at Assets/Scripts/WaveSpawner.cs:71)
Its difficult to know where the error occurred ( i need to see line numbers),, but But it seems you some how forgot to put some commands inside the 1st loop function
so Try this:
IEnumerator SpawnWave ()
{
for (int i = 0; i < waveIndex; i++)
{
_shop.GetComponent<$$anonymous$$oney$$anonymous$$anager> ().add$$anonymous$$oney (50);
Debug.Log ("Wave Inco$$anonymous$$g!");
GameObject.Find("EnemyTest").GetComponent<EnemyTest>().maxHealth += 10;
GameObject.Find("EnemyTest").GetComponent<EnemyTest>().health += 10;
waveIndex++;
}
for (int i=0;i<normalEnemyNumber[waveIndex];i++)
{
spawnEnemy();
yield return new WaitForSeconds(1f);
}
for (int i=0;i<bossEnemyNumber[waveIndex];i++)
{
spawnBoss();
yield return new WaitForSeconds(5f);
}
}
Without knowing what line 137 is, it becomes difficult for us to debug your code. This question, and questions like it, all point to a lack of understanding of program$$anonymous$$g concepts. I'd suggest taking a step back and watching some tutorial / live training videos on Unity's learn section, honestly.
@ JEFFDOG11111 Your third version won't work (it only changes the values of the local ints).
oh yeah ,, your right.. what was i thinking, better stick to the first 2 versions
Answer by YoloJoe · Sep 07, 2016 at 11:17 PM
I fixed it by putting the add health stuff in the enemyHealth script in stead of the WaveSpawer. Thanks for helping me out again guys :)
Your answer