Objects not spawning in formation
Options
I use this code to spawn players and targets but somehow they are spawned out of place.Can someone help me?
<pre class="CodeBlock"><code>[PunRPC] public void RPC_SpawnTargets() { int i = 1; TargetFormation=GameObject.Find("Target Formation").GetComponent<Transform>(); foreach (Transform child in TargetFormation ) { GameObject target = PhotonNetwork.Instantiate(Path.Combine("Prefabs","Target"),child.transform.position,Quaternion.identity,0) as GameObject; target.name = "Target ("+i+")"; target.transform.parent = child; i++; } i = 0; } [PunRPC] public void RPC_SpawnPlayers() { int i = 1; PlayerFormation=GameObject.Find("Player Formation").GetComponent<Transform>(); foreach (Transform child in PlayerFormation ) { GameObject player =PhotonNetwork.Instantiate(Path.Combine("Prefabs","Player"),child.transform.position,Quaternion.identity,0) as GameObject; player.name = "Player ("+i+")"; player.transform.parent = child; i++; } i=0; }
0
Answers
-
Hi @degeta10,
I guess the foreach loop is just running once as the object 'PlayerFormation' can just have one Transform component. Please check how many iterations there are, for example by using Debug.Log. Since I think you want to iterate through the child objects you would have to useforeach (Transform child in PlayerFormation.GetComponentsInChildren<Transform>()) { }
instead.0 -
Hi @Christian_Simon actually you should check this link too...it has more information.
https://stackoverflow.com/questions/49922729/objects-are-spawning-out-of-place0