Other
Hearth based health system help
I'm learning Unity, and I'm new to C#.
It's my first fully self made script and I want to know how to make it better and more optimized.
I think I should do something with part where script is looking for Animator in every Child.
As well as I want to add option to show two or more indicators, like health and shield in one "icon's bar"
Script
I apologize for mistakes, I'm not American.
You've attached a '.unitypackage' file, you should ins$$anonymous$$d attach your C# file.
@AidanHorton // I attached .unitypackage because it contains C# script, simple test assets and example scene
I don't have access to unity for a while, so attaching a C# script would have meant I could take a look at just the script now (since I have visual studio), but that's my fault for not mentioning that in my previous comment. Anyway it sounds like someone else with access to the full project would be more useful to you, good luck :)
I would have checked the script out but sorry I'm not gonna open unitypackages.
I understand it in some way, but why? Is it possible to put some sort of virus in .unitypackage file?
If you understand it then you know why. Some sort of virus? What's a virus other than a computer program and what's a C# file? It's basic online etiquette lol. Even if you don't know if it is possible to put a virus in a unity package, you don't open unknown files just like that, it is internet common sense. Why ask people to download and put your files in their computer when you could very well paste the code in this thread or put it on pastebin or anything like that. Of course people will be suspicious. Lol. Again, internet common sense dude.
Welcome to Unity! $$anonymous$$ost new developers share the same mistake that you did. Writing the script. On answers.unity3d.com, there is a feature where you can copy/paste your script to make it readable. Here is a tutorial:
This bar on the top has a feature which is the button. Click that, then a text box will come up and then you can paste your script there. Hope this helps!
@connorwforman : Even if you have been on Unity Answers for more than 6 months, and you know how to use the "101010" button, you still do not know when to post an answer or comment.
The script in general seems very well made. There's one or two small logic errors, and areas which could probably be improved performance wise, but apart from that it's very good for your first self-made script.
Good practice would be to mark all of the serialized variables as private.
A more efficient way of getting the current children would be using a list (https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx), so ins$$anonymous$$d of getting 'transform.getchild' you can get the child which would be stored in the list (this assumes you only add children in this script). Transform.child can be quite heavy so you should figure out a way to add and remove the correct amount of children from the list using the logic you already have.
Also the if statement around the first for loop is redundant, the for loop wont fire if childcount is greater than maxvalue because the 'i' would be greater than maxvalue-childcount.
The final thing I see is that you are not casting 'clone' to a gameobject, it would create an error if you tried to use it, so do this ins$$anonymous$$d:
GameObject clone = (GameObject)Instantiate(childPrefab, transform);
Thanks, I'll try lists. Do you have any tips how to make this script able to handle multiple "health types" (like live and shield)?
I'm going to need more detail on what exactly you need the script to do - I need specific detail here so I can help you with this
Follow this Question
Related Questions
Using downloaded image as UI Image's source image? 1 Answer
how do I rezolve "look rotation viewing vector is zero"? 1 Answer
Parse issue, despite the data being pulled from database 2 Answers
How to instantiate (MonoBehaviour) script multiple times and call void? 1 Answer
Location Does Not Change 1 Answer