Stupid question about script

Hi guys, as title says i have a stupid question:
I have two script, HealthScript and SoundScript.
Inside HealthScript for example:
if (health <= 0){
SoundScript.PlaySound()
}
Then inside SoundScript i have the function PlaySound()
This is one method, another one is to check if health goes below 0 inside the SoundScript like this:
if (HealthScript.health <=0){
PlaySound()
}
Which one is better for performance? I think the second one is more readable but less performing. I could also write all just inside the HealthScript but the code would be more messy. Any suggestion? Thanks :slight_smile:

First off, please use code tags as per the first post in the forum.

As far as how to do it for performance, and something like this would not have a measurable impact on performance, at least until you have a LOT of players in the game at once.

Generally you want to focus your code first on keeping “areas of concern” together.

That means, sound shouldn’t have to know about player health. Player health should know that.

2 Likes

Check this article out.
https://unity3d.com/how-to/architect-with-scriptable-objects

Or this video starting at 17:39

It is the exact same concept presented in different ways. I think it’s exactly what you’re looking for.

1 Like

^This

1 Like

So how i would play a sound when health goes below 0?

Thank you, i’ll watch it asap

Questions about performance aren’t stupid but it is important to understand that this is at best a micro-optimization that will have no visible effect even if one of them is faster than the other unless you’re performing it thousands of times per second.

2 Likes