![]() |
Attention | Topic was automatically imported from the old Question2Answer platform. |
![]() |
Asked By | flaae |
I have this Spatial node called Life where I have the viewport containing a TextureRect and a ColorRect to construct the lifebar. Finally, a Sprite3D to render the 2D image into the 3D world.
In the life script I have the following code to update the size of the lifebar according to the unit’s max health:
func calculate_size(value):
return Vector2(32 * value / 50.0, 32)
func set_max_life(value):
total_life = max(0, value)
current_life = total_life
var size = calculate_size(total_life)
$Viewport.size = size
$Viewport/LifeBox.rect_size = size
$Viewport/BarBox.rect_size = size
Then I have another 3D scene called Unit, that has Life as a child. Unit has the following script:
func _ready():
var max_life = 100
$Life.set_max_life(max_life)
Then, in my main scene I have one unit instantiated in the 3D editor, and another one through the following GDScript:
func _ready():
var nav = get_parent().get_parent() # Navigation Node
var unit_scene = preload("res://Unit.tscn")
for index in range(3):
var unit = unit_scene.instance()
var position = -(index + 1)
unit.translate(Vector3(position, .2, position))
unit.rotate_y(135 * PI / 180)
nav.call_deferred("add_child", unit)
But then, only the unit instantiated through the editor seems to have the lifebar size updated:
What am I doing wrong?