✅ 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
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
not working how?
$details
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)
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
nothing pops up in the log window?
atleast try sharing ur code so we can have some idea


sorry just got home

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) ..
like this

Not sure if that affects anything though
Nvm I am getting this now

yep thats the log window
seems to have solved it for me

$code
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/Thnks alot guys !
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.