- Home /
How can I have separate floats for each gameobject?
So, I am a newcomer unity Developer and I am facing a problem where I just learned ray casting, and I'm playing around with it. now that doesn't matter; I wanted to know how to have separate floats for each of the game objects which are my enemies. like, for example, I had two Enemies on one scene and I had a Health float one them and whenever I shoot them The health drops and they will be destroyed. but the problem is when one of them get destroyed the others die too. cause they have the same script on them and the same health float. how can I do something for this? I don't want to have one script for each and every one of my enemies. I want each of then to have Separate Health floats with one script. also my game is 3d haha. Thanks -a "newcomer who is not the best at English"
Well if each of the enemies have the same script, they still have different instances of the script. Therefore, the floats are already different. Your code might be the issue, and might be the reason all of their health values are edited simultaneously. Can I see it to verify? @glorifiednoobie
It will work as you want unless you made the floats static, can you show your script?
Answer by sacredgeometry · Dec 30, 2020 at 06:14 AM
Use instance members. Any instance member will be per component and your component lives on your game object so it will be per how ever many instances of that component live on your game object. if its one then its 1-1 with your game object i.e. one per gameobject.
Your answer
Follow this Question
Related Questions
What's wrong with my RaycastHit2D? 1 Answer
Another Raycast Issue. 0 Answers
Problem with raycasting, checking for ground doesn't work 3 Answers
(C#) Collider to collider2d 0 Answers
C# Raycast goes straight into the air 2 Answers