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 matsykun · Apr 30, 2012 at 07:00 PM · enemyspawnarraysmultiple

Spawn Multiple Monsters

Should be simple, but it's not as simple as I though. I have monsters in an XML list for waves of enemies, and I need to spawn multiple enemies per wave. However, when I do so with my current code, both spawn, but only one will spawn correctly, and he other will not move, nor will it spawn correctly. I think I need to call upon each element in the array, but I don't know how.

 public var spawnedMob:GameObject = null;
 public var monsterData:MobData = null;
 private var waveIndex:uint = 0;
 public var monsterPrefab:GameObject;
 private var wavesInfo:WavesData = null;
 private var wavesArray:Array = null;

 function Start () {
     monsterData = gameObject.GetComponent( MobData );    
     wavesInfo = gameObject.GetComponent( WavesData );
 }

 function DataReady(){
     wavesArray = wavesInfo.waves;
 }


 function Update () {
     if ( wavesArray ){
         if ( spawnedMob == null ){
             if ( waveIndex < wavesArray.length ){                
                 var currentWave:WaveInfo = wavesArray[waveIndex];                
                 var monsterSpawnIndex:float = parseFloat( currentWave.indexString );                    
                 var monsterInfo:MobInfo = monsterData.mobInfoArray[monsterSpawnIndex];                
                 var waveSplitter =  currentWave.indexString.Split(","[0]);
             
             for( var i:int = 0; i < waveSplitter.length; i++)
             {
                 print(waveSplitter[i]);
                 spawnedMob = Instantiate( monsterPrefab, Vector3( 100, 10, 100 ), Quaternion.identity );
             }
                             
             //Instantiate( monsterPrefab, Vector3( 80, 10, 80 ), Quaternion.identity );
             var entityScript:MonsterCharacter = spawnedMob.GetComponent( MonsterCharacter );                
             entityScript.InitWithMobInfo( monsterInfo );                
             spawnedMob.SendMessage( "SetPlayer", null, SendMessageOptions.DontRequireReceiver );                
             waveIndex++;
         }
     }
 }

}

Comment
Add comment · Show 1
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 matsykun · Apr 30, 2012 at 07:52 PM 0
Share

You are right, that was the problem. I fixed it, and I was also not calling the array where needed. If anyone is wondering for the fix:

 function Update () 

{ if ( wavesArray ) { if ( spawned$$anonymous$$ob == null ) { if ( waveIndex < wavesArray.length ) {
var currentWave:WaveInfo = wavesArray[waveIndex];

             var monsterSpawnIndex:float = parseFloat( currentWave.indexString );
             
             var waveSplitter =  currentWave.indexString.Split(","[0]);
             
             for( var i:int = 0; i < waveSplitter.length; i++)
             {
                 print(waveSplitter[i]);
                 
                 spawned$$anonymous$$ob = Instantiate( monsterPrefab, Vector3( 80*i, 10, 80 ), Quaternion.identity );    
                 
                 var monsterInfo:$$anonymous$$obInfo = monsterData.mobInfoArray[ parseInt( waveSplitter[i] ) ];
                 
                 var entityScript:$$anonymous$$onsterCharacter = spawned$$anonymous$$ob.GetComponent( $$anonymous$$onsterCharacter );
             
                 entityScript.InitWith$$anonymous$$obInfo( monsterInfo );
             
                 spawned$$anonymous$$ob.Send$$anonymous$$essage( "SetPlayer", null, Send$$anonymous$$essageOptions.DontRequireReceiver );
             
             }
             
             waveIndex++;
         }
     }
 }

}

1 Reply

· Add your reply
  • Sort: 
avatar image
1

Answer by fafase · Apr 30, 2012 at 07:18 PM

Have you tried to place all the codes you wrote after the for loop, inside the for loop, so that it is done right after instantiating. I would think the way it is done now, first round in the for loop, spawnemob points to a place in the memory then second round the same spawnmob points to a new location in memory then you fetch the component of that object pointed at but the firts one is lost in memory.

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Problem Spawning Enemys with Health. 2 Answers

JS to C# converting issues.. 1 Answer

An Array within array 2 Answers

How do I control where enemies spawn? 1 Answer

Spawning different random objects at the same position? 2 Answers


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