Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 jollz · Jun 29, 2013 at 09:12 PM · listspawnrespawnmonster

A respawn script for enemies.

Hey guys, I've run into somewhat of a wall one might call it. I have a simple respawn script for my enemies in my level (currently a very very simple one, for testing purposes).

I have all enemies in a list.

The script works by the killed enemy sending it's id and also it's respawn time to two separate lists.

The respawn script then checks the id against the list with enemies and when the corresponding respawn time reaches zero, the exact same monster which was killed is respawned.

It's working great, but I just recently found out that if an enemy with a shorter respawn time than what's left on the previously killed enemies respawn time the list goes out of range.

Could someone take a look at the code and see if you can find out what I'm missing.

(MonsterMaster, handles spawning initially and then respawning monsters defined in the inspector)

The code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class MonsterMaster : MonoBehaviour {
     
     public EnemyClass[] enemies;
     public List<float> respawnTime;
     public List<int> enemyIDs;
     
     void Start () {
         respawnTime = new List<float>();
         enemyIDs = new List<int>();
         
         for(int x = 0; x < enemies.Length; x++){
             if(enemies[x].isSpawned == false){
                 GameObject enemy;
                 enemy = Instantiate(enemies[x].enemyPrefab, enemies[x].spawnPoint, Quaternion.identity) as GameObject;
                 enemies[x].isSpawned = true;
                 Enemy enemyScript;
                 enemyScript = enemy.GetComponent<Enemy>();
                 enemyScript.enemyId = enemies[x].id;
                 enemyScript.health = enemies[x].health;
                 enemyScript.enemyName = enemies[x].name;
                 enemyScript.enemyGuild = enemies[x].guild;
                 enemyScript.level = enemies[x].level;
                 enemyScript.exp = enemies[x].experience;
                 enemyScript.moveSpeed = enemies[x].moveSpeed;
                 enemyScript.maxMoveDistance = enemies[x].maxMoveDistance;
                 enemyScript.respawnTime = enemies[x].respawnTime;
                 enemyScript.loot = enemies[x].loot;
             }
         }
     }
     
     void Update () {
         for(int x = 0; x < respawnTime.Count; x++){
             respawnTime[x] -= Time.deltaTime;
             for(int idCnt = 0; idCnt < enemyIDs.Count; idCnt++){
                 for(int cnt = 0; cnt < enemies.Length; cnt++){
 /*error here*/            if(enemyIDs.Count != 0 && enemies[cnt].id == enemyIDs[idCnt] && enemies[cnt].isSpawned == false && respawnTime[x] <= 0){
                         GameObject enemy;
                         enemy = Instantiate(enemies[cnt].enemyPrefab, enemies[cnt].spawnPoint, Quaternion.identity) as GameObject;
                         enemies[cnt].isSpawned = true;
                         Enemy enemyScript;
                         enemyScript = enemy.GetComponent<Enemy>();
                         enemyScript.enemyId = enemies[cnt].id;
                         enemyScript.health = enemies[cnt].health;
                         enemyScript.enemyName = enemies[cnt].name;
                         enemyScript.enemyGuild = enemies[cnt].guild;
                         enemyScript.level = enemies[cnt].level;
                         enemyScript.exp = enemies[cnt].experience;
                         enemyScript.moveSpeed = enemies[cnt].moveSpeed;
                         enemyScript.maxMoveDistance = enemies[cnt].maxMoveDistance;
                         enemyScript.respawnTime = enemies[cnt].respawnTime;
                         enemyScript.loot = enemies[cnt].loot;
                         enemyIDs.RemoveAt(idCnt);
                         respawnTime.RemoveAt(x);
                     }
                 }
             }
         }
     }
 }
 

The error:

ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[System.Single].get_Item (Int32 index) (at /Applications/buildAgent/work/84669f285f6a667f/mcs/class/corlib/System.Collections.Generic/List.cs:633) MonsterMaster.Update () (at Assets/Scripts/Enemies/MonsterMaster.cs:42)

(mainly focusing on functionality at the moment) The game in it's current state: https://dl.dropboxusercontent.com/u/163931410/Awesome/Awesome.html

Any tips for improvements are also highly appreciated!

Thanks for taking your time! :)

Comment
Add comment · Show 3
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 Benproductions1 · Jun 30, 2013 at 10:31 AM 0
Share

That error means you are trying to access something at an index of a list that does not exist.

 list = [0, 2, 6]
 lol = list[3]

In this case list[3] does not exist. Therefore the same error :)

avatar image jollz · Jun 30, 2013 at 10:38 AM 0
Share

Thanks for the quick reply!

I'm not sure how I'm going to solve this one however.

Example:

I kill enemy with id 2, got a respawn time of 10. It sends it's id to the ID List. It sends it's respawn time to the respawn List.

It's id should now be at index 0 of the id List and it's respawn time should now be at index 0 of the respawn List, correct?

If I then kill another enemy with id 3, respawn time of 2. It sends it's id to the same ID List, with index 1. It sends it's respawn time to the respawn List, index 1.

The first monster then spawns when the second monster's respawn time reaches 0 and the second monster spawns when the other respawn time reaches 0. (forgot to mention this in the question, sorry!)

So basically what's not working as I intended it to is the ID check.

Any help on this one is much much much appreciated!

Note: I'm very new to program$$anonymous$$g!

Thanks :)

avatar image Benproductions1 · Jun 30, 2013 at 11:45 AM 0
Share

I see nowhere where you add to the enemy id list. I'm guessing your problem is that your enemy id list is actually empty. Try just debugging the length of your lists and seeing if a problem arises there :)

1 Reply

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

Answer by jollz · Jun 30, 2013 at 12:08 PM

I just sat down and rewrote the whole thing and somehow I got it working properly!

Thanks Benproductions for the reply, and thanks to anyone who was trying to help but didn't get the time to reply!

Here's the rewritten code:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 [System.Serializable]
 public class MonsterMaster : MonoBehaviour {
     
     public EnemyClass[] enemies;
     public List<float> respawnTime;
     public List<int> enemyIDs;
     
     void Start () {
         respawnTime = new List<float>();
         enemyIDs = new List<int>();
         
         for(int x = 0; x < enemies.Length; x++){
             if(enemies[x].isSpawned == false){
                 GameObject enemy;
                 enemy = Instantiate(enemies[x].enemyPrefab, enemies[x].spawnPoint, Quaternion.identity) as GameObject;
                 enemies[x].isSpawned = true;
                 Enemy enemyScript;
                 enemyScript = enemy.GetComponent<Enemy>();
                 enemyScript.enemyId = enemies[x].id;
                 enemyScript.health = enemies[x].health;
                 enemyScript.enemyName = enemies[x].name;
                 enemyScript.enemyGuild = enemies[x].guild;
                 enemyScript.level = enemies[x].level;
                 enemyScript.exp = enemies[x].experience;
                 enemyScript.moveSpeed = enemies[x].moveSpeed;
                 enemyScript.maxMoveDistance = enemies[x].maxMoveDistance;
                 enemyScript.respawnTime = enemies[x].respawnTime;
                 enemyScript.loot = enemies[x].loot;
             }
         }
     }
     
     void Update () {
         for(int x = 0; x < respawnTime.Count; x++){
             respawnTime[x] -= Time.deltaTime;
             for(int respawnCnt = 0; respawnCnt < respawnTime.Count; respawnCnt++){
                 if(respawnTime[respawnCnt] <= 0){
                     for(int enemyCnt = 0; enemyCnt < enemies.Length; enemyCnt++){
                         if(enemies[enemyCnt].isSpawned == false && enemyIDs[respawnCnt] == enemies[enemyCnt].id){
                             Debug.Log (enemies[enemyCnt].id);
                             GameObject enemy;
                             enemy = Instantiate(enemies[enemyCnt].enemyPrefab, enemies[enemyCnt].spawnPoint, Quaternion.identity) as GameObject;
                             enemies[x].isSpawned = true;
                             Enemy enemyScript;
                             enemyScript = enemy.GetComponent<Enemy>();
                             enemyScript.enemyId = enemies[enemyCnt].id;
                             enemyScript.health = enemies[enemyCnt].health;
                             enemyScript.enemyName = enemies[enemyCnt].name;
                             enemyScript.enemyGuild = enemies[enemyCnt].guild;
                             enemyScript.level = enemies[enemyCnt].level;
                             enemyScript.exp = enemies[enemyCnt].experience;
                             enemyScript.moveSpeed = enemies[enemyCnt].moveSpeed;
                             enemyScript.maxMoveDistance = enemies[enemyCnt].maxMoveDistance;
                             enemyScript.respawnTime = enemies[enemyCnt].respawnTime;
                             enemyScript.loot = enemies[enemyCnt].loot;
                         }
                     }
                     respawnTime.RemoveAt(respawnCnt);
                     enemyIDs.RemoveAt(respawnCnt);
                 }
             }
         }
     }
 }
 
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 RemDust · Sep 04, 2015 at 03:30 PM 0
Share

awesome technic dude ! don't $$anonymous$$d if I ever come to use it ? I'm having such a hard time trying to write a respawn enemy system that satisfies me ^^'

avatar image jollz · Sep 06, 2015 at 07:28 PM 0
Share

@RemDust Feel free to use it as you please, do note however that it was written quite some time ago :)

avatar image RemDust · Sep 07, 2015 at 07:44 AM 0
Share

Actually, I don't like to use scripts I don't fully understand, and this is actually the case here ^^ but maybe if I don't find any other way to respawn my enemies properly soon, I'll come back to this.

Tank you so much !

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

16 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

Related Questions

A node in a childnode? 1 Answer

Character respawns invisable? 1 Answer

Spawning a new player instance (UNET) 3 Answers

How can I create New GameObject (Instantiate) 1 Answer

i need Spawn Object loop if find object tag ("Clone") in list ,when i delete find object tag ,cript is good but no find object, please help me 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