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 /
avatar image
0
Question by karaflito · Aug 05, 2017 at 09:27 PM · loop

Cant use foreach loop on my custome class

i am Trying to make a custom class, to arrange the wave of enemies more simple, so my class will set the enemies health,gold and it will be different for each wave. so i can modify and manage my enemies more properly, but i get an error and i dont know how to fix it. Here is my Code.

 public class  GameManagerScript : MonoBehaviour {
         // Created a Custom Class to make each wave different
         [System.Serializable]
         public class  EnemyManager  {
                         
         public GameObject enemy;
         public int maxEnemies;
         public float timebetweenspawns = 1.5f;
         public int EnemyGold;
         public int EnemyCurrentHealth;
             
         }
     
     public List <EnemyManager> EnemySpawn = new List<EnemyManager>();
         [SerializeField]
         private Transform SpawnPoint;
         private int waveLevel;
         private int totalSpawned = 0;
         private int AliveEnemies;
         private int enemiesIndex;
         private bool EnemiesNumeratorActive = false;
         [SerializeField] 
         private int CurrentGold;
     
     void Start () {
             UIText = FindObjectOfType<Text> ();
     
             EnemyHealth enemyhealth = gameObject.GetComponent<EnemyHealth> ();
     
             foreach (EnemyManager enemies in EnemySpawn[enemiesIndex].enemy) {
     
                 enemyhealth.AddHealth (EnemySpawn [enemiesIndex].EnemyCurrentHealth);
                 
             }
 
 
 
 
 
 
 

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
1

Answer by moltow · Aug 05, 2017 at 10:33 PM

I believe what you want is:

 foreach (EnemyManager enemy in EnemySpawn)
 {
     enemyhealth.AddHealth(enemy.EnemyCurrentHealth);
 }

Comment
Add comment · Show 3 · 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 moltow · Aug 06, 2017 at 01:54 AM 0
Share

The object reference not set to an instance of an object is likely because you're not populating your List. If your enemy GameObjects all have the same tag (e.g., "enemy"), then you can use GameObject.FindGameObjectsWithTag to populate your list.

avatar image karaflito moltow · Aug 06, 2017 at 09:27 AM 0
Share

So how my code should be. By finding the health script from each Enemy object in the list and add the EnemyCurrentHealth.

sorry i am a bit new to unity

avatar image moltow karaflito · Aug 06, 2017 at 05:21 PM 0
Share

Well I don't really know enough about your other classes to know that this is completely correct and I don't know the signature of your AddHealth method. Having said that, I would guess it would be something like this:

  void Start ()
         {
             UIText = FindObjectOfType<Text>();
     
             EnemySpawn = GameObject.FindGameObjectsWithTag("Enemy");
     
             foreach (Enemy$$anonymous$$anager enemy in EnemySpawn)
             {
                 EnemyHealth enemyhealth = enemy.GetComponent<EnemyHealth>();
                 enemyhealth.AddHealth(enemy.EnemyCurrentHealth + 5);
             }
         }

avatar image
0

Answer by karaflito · Aug 05, 2017 at 11:50 PM

unfortunately it doesnt work, it says, " object reference not set to an instance of an object"

what i want to do is to take the Enemy gameobject reference from the custom class and get its component and then send the currenthealth to that object.

so i try to use foreach loop to get every single enemy object from the list and add the health accordingly

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

68 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

Related Questions

Help With Add and Remove on a loop 2 Answers

"for" loop - DrawTexture problem. 0 Answers

problem with loop and mesh values 0 Answers

Crash on loop using Lerp. 1 Answer

Spawn Object 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