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();
}
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.
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);
}