Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 /
This question was closed Dec 05, 2019 at 09:21 PM by YJ8 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by YJ8 · Nov 30, 2019 at 04:50 AM · counterspawning problems

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



Comment
Add comment
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

1 Reply

  • Sort: 
avatar image
0

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'.

Comment
Add comment · Show 6 · 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
avatar image YJ8 · Nov 30, 2019 at 09:52 PM 0
Share

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

avatar image Larry-Dietz YJ8 · Nov 30, 2019 at 11:19 PM 1
Share

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.

avatar image YJ8 Larry-Dietz · Dec 01, 2019 at 06:38 PM 0
Share

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?

Show more comments

Follow this Question

Answers Answers and Comments

116 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Increase number of enemies spawned per level 0 Answers

Problems with instantiate 0 Answers

Having trouble accessing a variable in another class 1 Answer

Resouce script help 3 Answers

GuiText Score On Mouse Click 1 Answer


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