- Home /
Apply the same script on multiple objects
Hello! So I've been working on the basic mechanics of a top-down shooter game, and now I'm trying to apply some of the scripts and components that work for a single object to multiple objects. However, some of them do not work as expected. For example, I have a health bar UI (canvas in world space) that displays the enemy health. It works perfectly fine for a single enemy, but when I duplicate the enemy object, along with the canvas, the canvas does not function properly. I changed the references so that shouldn't be the issue. I'm wondering what's the problem here. Thank you! (As you can see in the pic, the second health bar does not behave according to the script)
If you changed references then the problem must be in your script. Each gameobject has an instance of a $$anonymous$$onobehaviour script, so they should be working independently (unless you're modifying some shared variables, like $$anonymous$$aterial).
Hi! Thank you for your response! What you said makes sense but I still don't know how to solve the problem. The image attached is my script for my health bar. The functions inside are called in my EnemyController script, which applies to multiple enemies. Could you take a look and possibly find the error?
And how do you call SetEnemy$$anonymous$$axHealth and SetEnemyHealth? Also what is the exact problem, because second screenshot shows two healthbars above enemies, is it that one is full health all the time and not working properly while the other one is fine?
Hi! Can you please be a bit more specific? The health bar script is already shown in the description, so do the respective calls to set enemy health.
Answer by xxmariofer · Aug 18, 2020 at 07:29 AM
hello, the none working one is a duplicate? instantiated at runtime or by code? my bet is that both EnemyHealthBar are referencing to the exact same objects can you add this method to that EnemyHealthBar script and test?
void Start()
{
slider = GetComponent<Slider>();
fill = transform.GetComponentInChildren<Image>();
}
Hi! Thank you for your response! I think what you said is exactly the problem. When the original enemy died (hence its health bar is also destroyed) and I shoot the duplicate enemies, error "$$anonymous$$issingReferenceException: The object of type 'Image' has been destroyed but you are still trying to access it" occurred. However, I still have no idea how to solve this other than changing references (which doesn't work). The healthbar is created in game, the code simply changes it.
Have you tested my Code? Should fix the errors
And how are you changing references? Double check if it's really pointing to 2 different health bars (especially the Fill image property). Cause it really does seem like both objects are pointing to the same object.
Shouldn't we avoid getcompent as much as possible to lower the burden of cpu/ improve the performance? PC aside, if I am porting my game to android and If I have lot of getcomponent, chances are it is going to affect the performance.
Adding "getcomponents" in the start of the game would never affect performance, only loading times. It is recommended to avoid getcomponents in update/fixedupdate/loops because thats called every frame.
Never be afraid of doing it in the starts/awake if that simplifies the code and working with the project, just to give you some numbers:
in this article you will see that calling 100.000 thousand times GetComponent taked him 20ms, even if you are targeting really low end devices, it is not common calling GetComponent more than 100 times unless is a big game and running the sample in a 4x times slower device it would take 0,0000008 seconds to do 100 getcomponent in a really low end device.
20ms overhead in the update is really high, but even for a loading window probably is imposible to notice.
Oh, good to know. that's great then! thank you.