Functions & FindObject alternative or keep? Enemy > Master Script > Player

Currently I have a targeting system that relies on OnCollision to then call a method on the GameManager then the Manager Script calling to the player. I’m satisfied with how the collisions are working at the moment but the way the structure is set up seems like it would be performance heavy if multiple enemies are in one scene. It looks something like this.

This function could be called multiple times in one scene because the player would be constantly switching targets

Enemy is hit by attack! > Passes Object Name of Enemy to Function in GameManager > Function then calls Function on the Player Script.and does its thing checking for positions etc.

Here are my concerns related to FindObject.

Each Enemy will need to have this in their Awake()
gameplayManager = GameObject.FindObjectOfType<scr_GameplayManager>();

Performance Based Questions:
1.Is FindObject only a problem when it’s being used to find different objects or the number of instances its used?
2.Is it better to create an Enemy Script containing AI based on what the enemy type is or create separate scripts?

3.Does creating multiple conditions for one command cause more performance issues then just running multiple commands? I.e. if(playerHealth <= 1 && canHeal == false || enemyThreat == true)

Oh yes, that’s silly, you called that right. There’s only ever one GM, right??

If so, use a singleton… here’s how:

Simple Singleton (UnitySingleton):

Some super-simple Singleton examples to take and modify:

Simple Unity3D Singleton (no predefined data):

Unity3D Singleton with Prefab used for predefined data:

These are pure-code solutions, do not put anything into any scene, just access it via .Instance!

The above contains DontDestroyOnLoad() calls. If you don’t want them “forever” remove that and they will last for this scene.

If you want it to last for all scenes during a game, leave the DDOL() in place, but then destroy it when the game is over.

1 Like

Thanks! I’ll check it out.