Enemy Spawning and Flow Error -> Alternate Spawner

Hello All,

I am up to the challenge at approx @15:00, and I have hit the wall regarding the spawning of enemy attackers.

At the moment all the lanes are only spawning the first member of the GameObjects array. For instance, in Lane 1, I made Lizzy first element and only that object spawns; and in Lane 2, I made FoxxyLoxxy the first element and that’s the only object that spawns and I can’t figure out why. The code does have a problem in that the first spawn is hardcoded rather than a variable, I wanted it that way though, I can sense that there is a problem in that only one timer is set instead of one for each object (maybe??), but, still can’t see through the fog.

And I don’t want the answer just hints. The code in Update, isTimeToSpawn and Spawn is as follows, as always please excuse the verbose comments it the way I script these days:

/// <summary>
/// Spawn (GameObject myGameObject)
/// </summary>
/// <param name="myGameObject"></param>
//spawn the object - easy peasy: clone=instantiate(object, possy,rotate)
public void Spawn(GameObject myGameObject)
{
    GameObject clone = Instantiate(myGameObject, possy, Quaternion.identity) as GameObject;

    print("myGameObject: " + myGameObject.name);
    //set the clone to the parent object:  Spawner_parent
    clone.transform.parent = Spawner_parent.transform;

    //This sets the timer 'x' seconds into the future again
    Timer = Time.time + myGameObject.GetComponent<Attacker>().seenEverySeconds; 

}


// Update is called once per frame
// need to do something here calling the Spawner with timer functions
// and whatnot to call whatever GameOject to be instantiated
void Update () {

    //using foreach method to cycle through the AttackerPrefabs array
    //foreach(Type, whatever name you choose, in ArrayName
    foreach(GameObject baddie in AttackerPrefabs)
    {
        //do something
        if(isTimeToSpawn (baddie) )
        {
            //do something
            //spawn the attacker
            Spawn(baddie);
        }//end if

    }//end foreach loop
	
}//end Update


bool isTimeToSpawn(GameObject attacker)
{
    //print("Baddie: "+attacker.name);
    //float getNumber;
    //getNumber = attacker.GetComponent<Attacker>().seenEverySeconds;
    //print("Get Number: "+getNumber);

    if (Timer < Time.time)
    { //This checks wether real time has caught up to the timer
        return true;
        
    }
    else
    {
        return false;
    }

    //return false;
}//end bool isTimeToSpawn;

If you are able to help me see the errors of my ways, just hints please, then here is a big thankyou in advance.

Regards,
Vaughan.

Addendum: I figured out what was wrong with my code the 2nd object was never getting set with a timer because there is only one timer in the code and that was being set with the first element.

So, I hacked out a solution based on the above that is really, really awful to look at aesthetically and to the mind and so not happy with it :frowning: . But it does work. It required knowing the GameObject by name in both the Update and in the Spawn method and then setting a specific timer for that object, not pretty at all.

I will hold off looking at the solution just in case someone gives me a hint that helps me do it better.

Regards.

Right, I had a look at the solution and bam - I couldn’t follow it at all - went right over my head, totally lost … I felt incredibly stupid that I couldn’t follow what he was doing. So, I said damn it, I have no clue what he’s doing, so, I have to fix up my code to do something like Ben was. So, step one, was first to just get it working like the example. Intellectually what I did didn’t sit well with me it was awful but it worked, so, I sat down and came up with an idea - and as I was going though the method of implementing it - my brain actually starting working, a rare occurrence, and the implementation became simpler and simpler - to the point where I said, “did I really do that”. Everything is automatic now, I could just put in more enemies set the seenInSeconds and it should work. At first I felt stupid because I couldn’t follow Ben’s code and then refined my way and now I feel like a game designer again :sunglasses:. This is the thing I love about this course :sunglasses::sunglasses::sunglasses:.

This is the for loop in Update:

   for(int i=0; i < arrayLength; i++)
   {
       if (isTimeToSpawnNew(AttackerPrefabs[i],i) )
       {
            SpawnNew(AttackerPrefabs[i],i);
       }//end if
    }//end for

isTimeToSpawn in it’s entirety (doesn’t even need the GameObject):

bool isTimeToSpawnNew(GameObject attacker,int i)
{
    if(timer[i] < Time.time)
    {
        //This checks whether real time has caught up to the timer
        return true;
    }
    else
    {
        return false;
    }
}//end bool isTimeToSpawn;

The main difference in Spawn:

public void SpawnNew(GameObject myGameObject, int i)
{
GameObject clone = Instantiate(myGameObject, possy, Quaternion.identity) as GameObject;

    print("myGameObject: " + myGameObject.name);
    //set the clone to the parent object:  Spawner_parent
    clone.transform.parent = Spawner_parent.transform;

    float midRange = myGameObject.GetComponent<Attacker>().seenEverySeconds;
    float hiRange = midRange + 2f;
    float lowRange = midRange - 2f;

    float rand = Random.Range(lowRange, hiRange);

    timer[i] = Time.time + rand;
}//end Spawn

Ooops, looks like a used a word, that wasn’t allowed. Sorry about that Mr Moderator. That’s a colloquialism used all over Australia and New Zealand.

Historically it was a term used against the Bogomil’s a religious sect for a practice apparently done against children as way to demonize the group, so, they could persecute and later exterminate the sect.

Sorry for crossing the line.

Privacy & Terms