I’ve been working on a drop and boost system in my game. When enemies die, some boxes drop and when player collides, boxes disappear and boost works.
Enemies are spawning from random spawn points and when there is no enemy on the scene, I cannot take the drop, in other words player cannot collide with boxes and boxes do not disappear. Also, I take this error on the console “object reference not set to an instance of an object”. I have tried to modify the script a couple of times but it did not work, unfortunately. You can see the last version of the script below.
private void OnTriggerEnter2D()
{
if (GameObject.FindGameObjectsWithTag(“Enemy”).Length >= 0)
{
FindObjectOfType<.Enemy>().enemySlowdownBoost(); (Problem is here, as console shows)
Destroy(gameObject);
}
}
I guess you mean FindObjectOfType<ThisScript>(), which is <Enemy.> script. As long as enemy respawns, yes it is active. That’s the problem because when there is no enemy in the scene and when I’m waiting for enemies to respawn, it gets inactive and I cannot collide with the boxes.
Basically FindObjectOfType<.Enemy>().enemySlowdownBoost(); tries to find the enemy script in the scene but it cannot find so it causes an error.
This line keeps throwing my own mental error warning
as read it’s telling me to find a function — enemySlowdownBoost() — on a object of type — dotEnemy
after looking at the API for OnTriggerEnter2D I would suspect you need to get a component rather than an object and then you run into the problem that the message is passing the trigger collider you dropped rather than the collider attached to the enemy.
note that collider is the trigger collider your player has encountered and that the variable name is not part of the Type.
also note that this function will only work if the “Enemy” component is attached to the same object as the trigger collider.
On the other hand, it appears you may be trying to call a function on each game object tagged as an enemy. If this is the case then you are missing a for each loop