x


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

}

more ▼

asked Apr 30, 2012 at 07:00 PM

matsykun gravatar image

matsykun
16 12 8 10

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 ( spawnedMob == 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]);

                spawnedMob = Instantiate( monsterPrefab, Vector3( 80*i, 10, 80 ), Quaternion.identity );    

                var monsterInfo:MobInfo = monsterData.mobInfoArray[ parseInt( waveSplitter[i] ) ];

                var entityScript:MonsterCharacter = spawnedMob.GetComponent( MonsterCharacter );

                entityScript.InitWithMobInfo( monsterInfo );

                spawnedMob.SendMessage( "SetPlayer", null, SendMessageOptions.DontRequireReceiver );

            }

            waveIndex++;
        }
    }
}

}

Apr 30, 2012 at 07:52 PM matsykun
(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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.

more ▼

answered Apr 30, 2012 at 07:18 PM

fafase gravatar image

fafase
23.9k 59 54 109

(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

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

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Topics:

x1031
x756
x695
x445

asked: Apr 30, 2012 at 07:00 PM

Seen: 1147 times

Last Updated: May 01, 2012 at 07:25 AM