Hey so I’ve been trying to spawn food at tagged elements. I’ve tagged the ones where the snake hasn’t yet reached as “Not Green” and the other ones “Green”. As the snake moves over the boxes the boxes turn green. I’m not able to figure out this logic as I’ve tried a couple of things and nothing has worked.
Some help would be really appreciated. I’m also not able to destroy the food after collision (isTrigger is set to true). The snake is on the far left.
This is the code I’ve typed out
Food.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Food : MonoBehaviour
{
[SerializeField] GameObject foodPrefab;
[SerializeField] Transform borderTop;
[SerializeField] Transform borderLeft;
[SerializeField] Transform borderDown;
[SerializeField] Transform borderRight;
// Start is called before the first frame update
void Start()
{
if(gameObject.tag == "Non Green"){
InvokeRepeating("SpawnFood", 3, 4);
}
}
private void SpawnFood() {
int x = (int) Random.Range(borderLeft.position.x, borderRight.position.x);
int z = (int) Random.Range(borderDown.position.z, borderTop.position.z);
Instantiate(foodPrefab, new Vector3(x, 0, z), Quaternion.identity);
}
void OnTriggerEnter(Collider collision){
Destroy(collision.gameObject);
}
}
Collision Handler.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class CollisionHandler : MonoBehaviour
{
private void OnCollisionEnter(Collision other) {
if(other.gameObject.tag == "Player") {
GetComponent<MeshRenderer>().material.color = Color.green;
gameObject.tag = "Green";
}
}
}