Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

InvokeRepeating problem - apple spawn in snake 2D game

Discussion in 'Scripting' started by Dexydon, Jan 3, 2016.

  1. Dexydon

    Dexydon

    Joined:
    Dec 20, 2015
    Posts:
    10
    Hello!
    I want to make food spawner but have problem with this script because it spawn food all time every 4 seconds.
    I want to have only one prefab in scene and when I destroy it then want to have another one.





    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3.  
    4. public class AppleSpawner : MonoBehaviour {
    5.  
    6.     // Food Prefab
    7.     public GameObject foodPrefab;
    8.    
    9.     // Borders
    10.     public Transform borderTop;
    11.     public Transform borderBottom;
    12.     public Transform borderLeft;
    13.     public Transform borderRight;
    14.    
    15.     // Use this for initialization
    16.     void Start () {
    17.         // Spawn food every 4 seconds, starting in 3
    18.        
    19.             InvokeRepeating ("Spawn", 4, 3);
    20.        
    21.  
    22.     }
    23.  
    24.     void Update()
    25.     {
    26.  
    27.     }
    28.    
    29.     // Spawn one piece of food
    30.     void Spawn() {
    31.         // x position between left & right border
    32.         int x = (int)Random.Range(borderLeft.position.x+1f,
    33.                                   borderRight.position.x-1f);
    34.        
    35.         // y position between top & bottom border
    36.         int y = (int)Random.Range(borderBottom.position.y+0.5f,
    37.                                   borderTop.position.y-0.5f);
    38.        
    39.         // Instantiate the food at (x, y)
    40.         Instantiate(foodPrefab,
    41.                     new Vector2(x, y),
    42.                     Quaternion.identity); // default rotation
    43.     }

    I tried already with this:
    Code (CSharp):
    1. if (GameObject.Find ("apple(Clone)") == null) {
    2.             InvokeRepeating ("Spawn", 1, 1);
    3.   }
    but not working ;/
    Thanks for help!
     
  2. Zacocast

    Zacocast

    Joined:
    Jun 20, 2015
    Posts:
    10
    You would have to add the null checker part of the scrip[t to your Invoke Method. Something like this:
    Code (CSharp):
    1.   void Spawn() {
    2.         // x position between left & right border
    3.         int x = (int)Random.Range(borderLeft.position.x+1f,
    4.                                   borderRight.position.x-1f);
    5.  
    6.         // y position between top & bottom border
    7.         int y = (int)Random.Range(borderBottom.position.y+0.5f,
    8.                                   borderTop.position.y-0.5f);
    9.  
    10.         // Instantiate the food at (x, y)
    11. if (GameObject.Find ("apple(Clone)") == null) {
    12.         Instantiate(foodPrefab,
    13.                     new Vector2(x, y),
    14.                     Quaternion.identity); // default rotation
    15.          }
    16.     }
    This new code should work!
     
    Dexydon likes this.
  3. Dexydon

    Dexydon

    Joined:
    Dec 20, 2015
    Posts:
    10
    Thank you so much! :D
     
    Zacocast likes this.
  4. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
    You should avoid using destroy and practice with recycling objects. Just turn the object off when it is picked up and check every x seconds if it is activated or not. If not, you simply turn it on again.
     
  5. Zacocast

    Zacocast

    Joined:
    Jun 20, 2015
    Posts:
    10
    Yeah I was going to mention this to but I figured I might as well solve your original problem first. This is a good idea though, and it also saves on a lot of game memory because it doesn't have to run as many commands.