Transform.parent question!

Hi !

here I would like to ask about transform.parent.

public GameObject enemyPrefab;

void Start () {
	GameObject enemy1 = Instantiate(enemyPrefab, new Vector3(0,0,0), Quaternion.identity) as GameObject;
	enemy1.transform.parent = transform;

}

}

from script above,

GameObject enemy1 = Instantiate(enemyPrefab, new Vector3(0,0,0), Quaternion.identity) as GameObject; 
// Instantiate enemyPrefab from my Prefab assets folder by putting my prefab enemy sprite to the  GameObject slot on Inspector and we declare that as name enemy1 which is GameObject type and we make that as a GameObject?
		
enemy1.transform.parent = transform;
// enemy1 's parent which is currently EnemyFormation(Because we attached the prefab which then became gameobject(enemy1) 's position becomes the position(transform) of enemy1 ??

this part, I am bit confused. Please correct my description above if it is wrong. Thank you!

2 Likes

You are instantiating the whole GameObject, the same as if you drag the prefab to the hierarchy. It will be instantiated without an parent and then in the second line of code you are saying that the GameObject parent will be the current script’s GameObject which seems to be the formation (in order to make it inherits the formation movements)

1 Like

Thanks for this. Totally understood!

1 Like

Privacy & Terms