- Home /
The question is answered, right answer was accepted
How to spawn object when the Count of that object is 0
Hello, I'm learning how to code simple snake game from (https://noobtuts.com/unity/2d-snake-game). I'm trying to spawn a food when the foodCount = 0 but it doesn't spawn it. What is wrong with the code?
in SpawnFood.cs:
public class SpawnFood : MonoBehaviour {
public GameObject foodPrefab;
private static int foodCount = 0;
private const int maxFoodCountInScene = 4;
public static void EatOne()
{
if (foodCount >= 1)
{
foodCount--;
}
}
// Start is called before the first frame update
private void Start()
{
InvokeRepeating("Spawn", 3, 4);
}
private void Spawn()
{
if (GameController.isPaused) return;
int x = (int)UnityEngine.Random.Range(borderLeft.position.x, borderRight.position.x);
int y = (int)UnityEngine.Random.Range(borderBottom.position.y, borderTop.position.y);
if ( ( foodCount < maxFoodCountInScene) || (Convert.ToInt32(foodCount) == 0) )
{
Instantiate(foodPrefab, new Vector2(x, y), Quaternion.identity);
foodCount++;
}
}
and In Snake.cs:
public class Snake : MonoBehaviour{
.
.
.
.
.
.
.
private void OnTriggerEnter2D(Collider2D collision)
{
if (collision.name.StartsWith("Food"))
{
ate = true;
Destroy(collision.gameObject);
SpawnFood.EatOne();
}
else if(collision.tag.Contains("Border"))
{
GameController.FailGame();
//TODO:YOU LOSE SCREEN
}
}
}
Answer by oTaijjo · Nov 30, 2019 at 11:50 AM
If you really only want to spawn food, when foodCount is 0, then you need to change this line
if ( ( foodCount < maxFoodCountInScene) || (Convert.ToInt32(foodCount) == 0) )
to this
if ( foodCount == 0 )
In your version you're saying: 'spawn food, when foodCount is smaller than maxFoodCountInScene (i.e. 4) OR if foodcount is 0'.
I wanted both, spawn when less than 4 (max of food is 4 is game) and when its 0 , spawn food.. But when food is 0, it doesnt spawn in game. I suspect it has to do with my counter not working properly or i can't do that with invoke-repeating
Technically, that 2nd check for 0 is redundant. 0 is less than maxFoodCountInScene, presumably, so it should spawn at any number smaller than maxFoodCountInScene, including 0
If you think your count is somehow getting off, add a Debug.Log to the Spawn routine.
Something like this... Debug.Log("Current Food Count: "+foodCount+" $$anonymous$$ax food allowed: "+maxFoodCountInScene);
That way you can look in the console and see if the numbers are what you expected them to be.
I just debug and check my count and its working. It's spawning but because i used invoke repeating every 4 sec, it only spawn after 4 sec. What I want is for it to spawn every 4 sec and when its 0 foodCount spawn immediately. Can i do that with invoke-repeating or do I have to used invoke or other method?
Follow this Question
Related Questions
Increase number of enemies spawned per level 0 Answers
Run time counter 1 Answer
Only tagged objects raise counter 1 Answer
Making something happen only for one second during an Update function? 1 Answer
Creating an up and down counter 2 Answers