NullReferenceException:Object reference not set to an instance

Hi,

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

Can you help me to fix this issue, please?

Thanks for the help.

Hi,

FindObjectOfType() is not complete. You need to define the type you are looking for: FindObjectOfType<YourScript>().


See also:

Hey Nina,

Actually, the script is in FindObjectOfType<YourScript>() type. I wrote here in the right format but it did not appear.

Is there an active instance of that script/class in your scene?

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.

Yes, that’s very likely the problem. Why do you even look for “some Enemy” and not for an Enemy component attached to the colliding game object?

I have tried to look for the Enemy component in the first place but it did not work so I have changed it to this version.

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.

The solution you offered is not working actually. When I use GetComponent<.Enemy>().EnemySlowdownBoost(); boost does not even collide with the player.

If I’ve parsed your code correctly this is what you have.

        private void OnTriggerEnter2D(Collider2D collider)
        {
            if (GameObject.FindGameObjectsWithTag("Enemy").Length >= 0)
            {
                collider.GetComponent<Enemy>().EnemySlowdownBoost();
                Destroy(gameObject);
            }
        }

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

        private void OnTriggerEnter2D(Collider2D collider)
        {
            GameObject[] enemies = GameObject.FindGameObjectsWithTag("Enemy");

            if (enemies.Length >= 0)
            {
                foreach (GameObject enemy in enemies)
                {
                    GetComponent<Enemy>().EnemySlowdownBoost();
                }
            }
            Destroy(gameObject);
        }

This topic was automatically closed after 14 days. New replies are no longer allowed.

Privacy & Terms