Hi guys, first thing first. I have this code:
using UnityEngine;
using System.Collections;
public class SpawnActor : MonoBehaviour {
public GameObject actorPrefab;
private float spawnTime = 0.5f; //0.5 secs to spawn each
private float spawnCounter = 0.0f;
private int actorCounter = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
spawnCounter += Time.deltaTime;
if (spawnCounter > spawnTime)
{
spawnCounter -= spawnTime;
Spawn ();
}
}
void Spawn()
{
GameObject actor = (GameObject) Instantiate(actorPrefab);
actor.name = actor.name + "_" + (++actorCounter).ToString("D3");
actor.transform.position = Vector3.zero;
actor.transform.rotation = Quaternion.identity;
actor.animation.Play("Take 001");
StartCoroutine(DeletionCoroutine(actor, actor.animation["Take 001"].length));
}
IEnumerator DeletionCoroutine(GameObject actor, float time)
{
yield return new WaitForSeconds(time);
Debug.Log ("Actor to be deleted: " + actor.name);
Destroy(actor);
}
}
This is just a simple spawning script which will create a gameobject every 0.5 secs, play a pre-determined animation a-la cutscenes (just a simple walking forwards a few steps actually), and then deleted it at the end of the animation.
Now the problem is, every now and then, a few of the objects will be culled and not rendered at all (maybe because the time interval is too short and the objects are close to one another?). The reason I know that it is culled rather than destroyed was because I have this line in the Spawn() function:
actor.name = actor.name + “_” + (++actorCounter).ToString(“D3”);
And this line in the DeletionCoroutine() function:
Debug.Log ("Actor to be deleted: "+ actor.name);
And the order of the actor was actually correct, so I assume the order of the deletion itself must have been correct also.
Now the question, why did the object not rendered? How do I solve this? BTW, I’ve tried the “Always Rendered” in the animation’s culling type to no avail.
Thanks in advance.
EDIT:
I forgot some more info, if that will be useful. The GameObject doesn’t have a collider at all. And actuallly I forgot to add one more line in the Spawn() function. It actually has this: actor.animation[“Take 001”].speed = 2.0f;. Or in a way, the animation is played at twice the speed.