I have a small problem I can’t seem to solve on my own. When the enemy collides with the food the food should delete and respawn somewhere else. This works with the player but not for the enemy. I have already checked tag names and they are the same. I also am getting no errors.
//Variables
public Transform food;
public Vector3 spawnSpace;
private int x;
private int z;
public bool called = true;
public float timer;
//Functions
private void CreateFood()
{
print("Attempt to create");
x = Random.Range(-7, 8);
z = Random.Range(-7, 8);
Vector3 spawnSpace = new Vector3(x, 0.5f, z);
Instantiate(food, spawnSpace, Quaternion.identity);
}
private void OnTriggerEnter(Collider other)
{
if ((other.tag == "Player") || (other.tag == "Enemy"))
{
Destroy(this.gameObject);
CreateFood();
//works for Player but not Enemy, is always called when Player collides with food
print("Collided");
}
}
You are on the right track… but add more print() statements. How about one at line 21 to print what other.tag is!
And then if it’s not even calling the trigger function, time to go see the trigger function docs and make sure you are meeting 100% of the requirements to get called. SOMEBODY in that collision has to have a rigidbody, so if your enemies don’t have a rigidbody, that would certainly cause this behavior.
What is often happening in these cases is one of the following:
the code you think is executing is not actually executing at all
the code is executing far EARLIER or LATER than you think
the code is executing far LESS OFTEN than you think
the code is executing far MORE OFTEN than you think
the code is executing on another GameObject than you think it is
To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.
Doing this should help you answer these types of questions:
is this code even running? which parts are running? how often does it run? what order does it run in?
what are the values of the variables involved? Are they initialized? Are the values reasonable?
are you meeting ALL the requirements to receive callbacks such as triggers / colliders (review the documentation)
Knowing this information will help you reason about the behavior you are seeing.
You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene
You could also just display various important quantities in UI Text elements to watch them change as you play the game.
If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.
Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:
ALSO: if you are moving a Rigidbody-equipped object by setting the transform.position (or transform.Translate()), that won’t work. You are effectively bypassing the physics system and you will miss calls.
Instead, always use the .MovePosition() and .MoveRotation() methods on the Rigidbody instance to move it.