Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

242
Vistas
How do I spawn only 1 apple on collision

I am new to unity as a whole and decided to create small simple snake game as a test. I managed to create movement, the problem now is I want to eat the apple first, then wait till I eat the apple, then spawn a new one.

So far I managed to make it wait till I eat the first apple but then it spawns a lot of apples all at once, how do I make it that It waits till I eat the apple then spawns one apple and then another apple so far and forth.

I made only 1 apple spawn in a set location so far because I wanted to test it out before spawning in another location.

Here is the code I got so far

void OnTriggerEnter2D(Collider2D other)
 {
     Destroy(gameObject);
 }
 public void checkApple()
 {

     
         if (GameObject.Find("Apple"))
         {
             Debug.Log("The apple has not been eaten");
         }

         else
         {
             Debug.Log("The apple has been eaten!");
             GameObject a = Instantiate(Apple) as GameObject;
             a.transform.position = new Vector2(-10.26f, 3.66f);
         }
     
             
 }

 void Update()
 {
     checkApple();
 }
over 4 years ago · Santiago Trujillo
2 Respuestas
Responde la pregunta

0

While there are better methods to handle your problem than to search your entire scene for an object name a solution to your problem would look as followed: Currently you instantiate a new Object after you destroyed the old one, while your prefab or object might be called "Apple" the new Instance will be called "Apple(Clone)" and as you are only searching for the object that is called "Apple" and not for an object that contains the name Apple it will never find an object.

To avoid that you would need to set the name of the newly created object. For that you only need to rename the created object

 if (GameObject.Find("Apple"))
 {
    Debug.Log("The apple has not been eaten");
}

else
{
    Debug.Log("The apple has been eaten!");
    GameObject a = Instantiate(Apple) as GameObject;
    a.name = "Apple";
    a.transform.position = new Vector2(-10.26f, 3.66f);
}

However if your final goal is to "create a new apple on a new position" it would be better to just move the object to the new position when your snake collides with the apple, if you dont want the apple to instantly spawn you could create a cooldown timer something like this:

void OnTriggerEnter2D(Collider2D other)
 {
     //when needed make sure that  the snake only eats apples
     //if(other.name.Equals("Apple")){
       //for a random position you could use
       // Apple.transform.position = new Vector2(Random.Range(-10.0f, 10.0f),Random.Range(-10.0f, 10.0f));
       Apple.transform.position = new Vector2(-10.26f, 3.66f); 
       Apple.SetActive(false);
       cooldown = 2f;
       inactiveApple = true;
     //}
 }

public void checkApple()
{
   cooldown -= Time.deltaTime;
   if(cooldown <= 0 && inactiveApple)
   {
      Apple.SetActive(true);
      inactiveApple = false;
   }
}

Of course in the end you would need to check first if the new position is occupied by the snake befor you spawn it there, but that's off topic.

over 4 years ago · Santiago Trujillo Denunciar

0

You can use invoke to wait;

float waitingTime = 1f;

void Start()
{
     Spawn();
}

void OnTriggerEnter2D(Collider2D other)
 {
     Destroy(gameObject);
     Invoke("Spawn", waitingTime);
 }


 void Spawn()
 {
    Debug.Log("The apple has been spawned!");
    GameObject a = Instantiate(Apple) as GameObject;
    a.transform.position = new Vector2(-10.26f, 3.66f);
 }
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda