I actually tried using another approach. I attached a script to VBoxContainer which I have named HeartBox and within that script I have transferred the function :
extends VBoxContainer
func heartDisplay():
print('HeartBox')
var heart = TextureRect.new()
heart.texture = load("res://Textures/Heart.png")
add_child(heart)
and now In the other script I am calling this function by using
var heartBoxReference = load("res://MainScenes/HeartBox.gd").new()
This is how I call heartDisplay() :
heartBoxReference.heartDisplay()
The call is successful as HeartBox gets printed but it seems lie add_child(heart)
is still not working ! I get no errors now and if I call heartDisplay() where I have declared it i.e in VBoxContainer Script, then it works. But then the heart texture gets added only in the beginning in _ready() function and several times in _process() function which is not strange given the nature of these functions. What's strange is, why this function not adding texture when I call it from other script?