I have two types of boats in my 2D game. I’m cloning boats and sending to another side of screen. First type works fine, but second no. When i use breakpoints to find problem, i clone first boat of sekond type fine, but second was cloned to same x coordinates. I check array with childs transforms and everythink was null.
First type script:
using UnityEngine;
using System.Collections;
public class lod_normal_vpravo : MonoBehaviour
{
public static bool spawnship;
static Transform[] childs;
// Use this for initialization
void Start ()
{
childs = new Transform[40];
}
// Update is called once per frame
void Update ()
{
if (spawnship) {
SpawnChild ();
}
for (int i = 0; i<childs.Length; i++) {
if (childs *!= null) {*
childs .Translate (Vector3.left * Time.deltaTime, Camera.main.transform);
if (childs .position.x < 0 - childs .renderer.bounds.size.x) {
_ Destroy (childs .gameObject);
childs = null;
* }
}
}
//transform.Translate (Vector3.left * Time.deltaTime, Camera.main.transform);
}*_
* void SpawnChild ()*
* {*
* bool volno = true;*
* int i = 0;*
* while (volno) {*
_ if (childs == null) {
* volno = false;
} else {
if (i == 39) {
return;
}
i++;
}
}*_
_ childs = Instantiate (transform) as Transform;
* float position = Random.Range (6.5f, 9.8f);
childs .localPosition = new Vector3(childs .position.x, position, childs .position.z);
spawnship = false;
}
}*_
Second type script:
using UnityEngine;
using System.Collections;
public class lod_normal_vlevo : MonoBehaviour {
* public static bool spawnship;*
* Transform[] childs;*
* // Use this for initialization*
* void Start () {*
* childs = new Transform[40];*
* }*
* // Update is called once per frame*
* void Update () {*
* if (spawnship) {*
* SpawnChild ();*
* }*
* for (int i = 0; i<childs.Length; i++) {*
_ if (childs != null) {
childs .Translate (Vector3.right * Time.deltaTime, Camera.main.transform);
if (childs .position.x > Screen.width) {
Destroy (childs .gameObject);
childs = null;
* }
}
}
}*_
* void SpawnChild ()*
* {*
* bool volno = true;*
* int i = 0;*
* while (volno) {*
_ if (childs == null) {
* volno = false;
} else {
if (i == 39) {
return;
}
i++;
}
}*_
_ childs = Instantiate (transform) as Transform;
* float position = Random.Range (6.5f, 9.8f);
childs .localPosition = new Vector3(childs .position.x, position, childs .position.z);
spawnship = false;
}
}*_