How to get child from instantiate

Hi guys

I’m new to Unity javascripting, follow a few tutorials on youtube, pretty much frankensteined my way through the scripts. Now I have a problem with instantiate child

I’ve created an Enemy_Piggy_01 which have a child Tear which is slightly down the children hierarchy.

I want to have the piggy drip the tear when its killed so I have a working script

var tear = GameObject;

function Start(){

tear = GameObject.Find("Tear");

tear.renderer.enabled = false;
}

function update(){

if (health < 1){

health = 0;

teaer.renderer.enabled = true;
}

}

now this works fine when it is single object. So I throw Enemy_Piggy_01 into a prefab Enemy_Piggy_Prefab_01. Created an empty game object called Enemy_Piggy_Spawner_01 with the spawn script.

var enemyPrefab = GameObject;

function Start(){

spawn = true;

function Update(){

if (spawn)

{ Spawn();}

function Spawn()

{ Instantiate(enemyPrefab, transform.position, transform.rotation);}

This is not the full spawning script but anyway spawning works fine, pick the Enemy_Piggy_Prefab_01 as enemyPrefab . But now the Tear on the spawned objects is not getting the

tear.renderer.enabled = false;

I believe because tear = GameObject.Find(“Tear”); is wrong I’ve tried a few I read from this forum

tear = gameObject.Find(“Tear”).transform;

tear = Enemy_Piggy_01.transform.Find(“Tear”);

all doesn’t work … PLEASE HELP!!!

Thnx~ :smiley:

First, please format your code.

Second, tear = Enemy_Piggy_01.transform.Find("Tear");

is the right answer, but your path might be wrong.

Look at the example at Transform.Find.

If your “tear” is not directly under the piggy prefab, you need to write a relative path.

for example:

tear = Enemy_Piggy_01.transform.Find("PigHead/Face/Eye/Tear");

Thats all :slight_smile: