Spawner.cs with an enemy currency


#1

Hello everyone.
Yesterday I came back on the Glitch garden project because I want to change some game Mechanics.
I want to add an enemy currency that with every attacker spawn it decrease and when it reaches 0 or a positive value near that it stops spawning. I want to add it because with the spawn timer the game spawns infinite enemies (it stops only when the game stops) so there will be only a predefined max number of enemies (if strat money is 500 and and the lizard cost 10 and the fox 5, there could be max 100 foxes or 50 lizards or some random number of foxes and lizards that costs 500 coins)
In the Attackers.cs script I’ve added a public int value called SpawnCost (public int SpawnCost)
and in the Spawner.cs in “if (IsTimeToSpawn(thisAttacker)” script I’ve added &&StartSpawnMoney-spawnCost>=0) StarSpawnMoney is the total money in the level (it’s a public int setted at 500 by default)
so now it looks like this if (IsTimeToSpawn(thisAttacker)&&StartSpawnMoney-spawnCost>=0)
I know that I’ve to add int spawnCost = attackers.SpawnCost; somewhere in the spawner code but I don’t know where. this is my Spawner.cs script

public class Spawner : MonoBehaviour {
    public GameObject[] attackerPrefabArray;
    public int StartSpawnMoney = 500;
    public int SpawnCost;
    private Attackers attackers;
	// Use this for initialization
	void Start () {
        
    }
	
	// Update is called once per frame
	void Update () {
		foreach(GameObject thisAttacker in attackerPrefabArray)
        {
            if (IsTimeToSpawn(thisAttacker)&&StartSpawnMoney-spawnCost>=0)
            {
                Spawn(thisAttacker);
            }
        }
        Debug.Log(StartSpawnMoney);
	}
    void Spawn(GameObject myGameObject)
    {
        GameObject myAttacker = Instantiate(myGameObject) as GameObject;
        myAttacker.transform.parent = transform;
        myAttacker.transform.position = transform.position;
    }
    bool IsTimeToSpawn(GameObject attackerGameObject)
    {
        Attackers attackers = attackerGameObject.GetComponent<Attackers>();
        float meanSpawnDelay = attackers.seenEverySeconds;
        int spawnCost = attackers.SpawnCost;
        float spawnsPerSecond = 1 / meanSpawnDelay;
        if (Time.deltaTime > meanSpawnDelay)
        {

        }
        float threshold = spawnsPerSecond * Time.deltaTime/5;
        return (Random.value < threshold);
       
    }
}

I’ve tried to add it in the IsTimeToSpawn method but it’s not ok, neither in the Update method.
Thank you and sorry for my bad English. :slight_smile:


#2

The problem isn’t (just) a matter of timing. It’s that you are declaring the variable twice. The variable is declared once as a class variable then again in IsTimeToSpawn() as a local variable, meaning it creates a new variable with the same name. The local variable is separate from the one used by the rest of the class.

Change:
int spawnCost = attackers.SpawnCost;
to simply:
spawnCost = attackers.SpawnCost;


#3

thank you very very very much :slight_smile: it works now
Edit: It doesn’t work. Enemies spawn even if they have 0 money


#4

Maybe because of the >= you have in that above code example? Try changing it to >

if (IsTimeToSpawn(thisAttacker)&&StartSpawnMoney-spawnCost>=0)    // <-- this line
{
    Spawn(thisAttacker);
}

#5

I Just found out that it works but it gives 500 Money for every lane. I’ve set the StartSpawnMoney at 20 and I saw that only 2 lizards where spawning per lane (a lizard costs 10). Maybe I should do a new script for the Spawner parent image that holds the money for every lane.


#6

I did wonder about this when I read your original post the other day, but as it was all in hand I didn’t chime in.

Yes, you have multiple lanes, each lane has a spawner, thus you have the 500 value 5 times. A single instance of this would be better as you can be sure of the total at any given time. An alternative may be to make the money variable static, it will then belong to the class and not an instance of the class, but whilst this would work I think it would make things more complicated, something which this doesn’t need.


#7

This topic was automatically closed 24 hours after the last reply. New replies are no longer allowed.