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 /
avatar image
-1
Question by YoloJoe · Sep 07, 2016 at 08:15 PM · intaddinghealth

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? :)

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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;









Comment
Add comment · Show 5 · 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 YoloJoe · Sep 07, 2016 at 08:35 PM 0
Share

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)

avatar image JEFFDOG11111 YoloJoe · Sep 07, 2016 at 08:44 PM 0
Share

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);
         }
 
     }




avatar image Habitablaba YoloJoe · Sep 07, 2016 at 08:46 PM 0
Share

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.

avatar image Bonfire-Boy · Sep 07, 2016 at 08:38 PM 0
Share

@ JEFFDOG11111 Your third version won't work (it only changes the values of the local ints).

avatar image JEFFDOG11111 Bonfire-Boy · Sep 07, 2016 at 08:48 PM 0
Share

oh yeah ,, your right.. what was i thinking, better stick to the first 2 versions

avatar image
0

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 :)

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

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

56 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

Related Questions

Does my script become stupid, or do I become stupid? 1 Answer

A loop seems to do the function twice if the number is double digits? 1 Answer

How to Add time to my timer on colliding 1 Answer

Health Bar Help (2d, Unity 5) 1 Answer

Applying damage to the player health problem 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