Change level after killing specified number of enemies

I am trying to get the Level Manager to load next level after a set number of enemies are killed. I am getting an error that states, “KilledEnemies does not exist in current context.” The level manager sets the number of enemies to be killed. The projectile is to update the enemies killed and then, once the number is reached, a new level is loaded.

What I am trying to do in Projectile is call the Level Manager then call the component Killed Enemys and then increase it by one before destroying the object.

If there is a training video for this, please let me know.

Any help will be appreciated. Thanks.

Hi Nevik,

One of your issues is here;

Projectile.cs

void Start()
{
    killedEnemys = Gameobject.Find(KilledEnemys).GetComponent<LevelManager>();
}

What you are saying there is “Find me the GameObject called KilledEnemys, and then get a reference to a component attached to it named LevelManager”.

Where-as actually its the LevelManager GameObject you want to find, and killedEnemys is not a component, but a member variable of the LevelManager class.

void Start()
{
    levelManager = GameObject.FindObjectOfType<LevelManager>();

    killedEnemys = levelManager.KilledEnemys;
}

There are a number of things I might suggest changing with your approach, but perhaps initially, just get it to do what you want it to do and then consider improving it afterwards.

Not least of which is having this find operation on every projectile, find is quite expensive from a performance perspective, so having something run off to find the same thing ever time a projectile is created isn’t ideal. But one thing at a time. See what happens with the above changes.

Hope this helps.

Also, please copy/paste your code into your posts and apply code formatting (see below) rather than using screenshots, it is much more difficult for people to help you with screenshots as they have to type out all of the code themselves when making suggestions, rather than just copy/pasting part of your code back to you in a reply.

Screenshots are really useful for details from the Unity editor or errors though. :slight_smile:


See also;

Privacy & Terms