C
C#2y ago
henke

✅ beginner help with unity

Deal: I am trying to take my health float value from a script that is "not" in the scene (just a script) and pass it the child of a Player Gameobject. I have tried to use GetComponent and some basic stuff but i dont know why its not working. I can use GetComponent from my Player object and get the health float but not from the chiöd script. Any help or advice appreciated!
17 Replies
Anton
Anton2y ago
transform.GetChild(0).gameObject.GetComponent<...>(); this reaches into the hierarchy and gets the component from the first child or if you need the one on the parent transform.parent.gameObject.blah
TheRanger
TheRanger2y ago
not working how? $details
MODiX
MODiX2y ago
When you ask a question, make sure you include as much detail as possible. Such as code, the issue you are facing, and what you expect the result to be. Upload code here https://paste.mod.gg/ (see $code for more information on how to paste your code)
henke
henkeOP2y ago
thanks i will test it now Sorry im not sure. there are no error lines but when I run the script I can also tell that there is nothing coming from the lines that I wrote
TheRanger
TheRanger2y ago
nothing pops up in the log window? atleast try sharing ur code so we can have some idea
henke
henkeOP2y ago
henke
henkeOP2y ago
sorry just got home
henke
henkeOP2y ago
you can see here the HealthBars gameObject is a child of "Player" and DamageableCharacter.cs is just a script, not attached to any gameObject (though referenced) ..
henke
henkeOP2y ago
like this
henke
henkeOP2y ago
Not sure if that affects anything though Nvm I am getting this now
henke
henkeOP2y ago
TheRanger
TheRanger2y ago
yep thats the log window
henke
henkeOP2y ago
seems to have solved it for me
Anton
Anton2y ago
$code
MODiX
MODiX2y ago
To post C# code type the following: ```cs // code here ``` Get an example by typing $codegif in chat If your code is too long, post it to: https://paste.mod.gg/
henke
henkeOP2y ago
Thnks alot guys !
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server