Hello,
I have some code basically the code calls a public function to instantiate a prefab with a bunch of gameobject animations. For some reasons, I am not sure why multiple objects aren’t created. It works for the first but not the rest. I’m at a lost. Could someone help The function PlayAnimation() is called to instantiate also it calls Stop() before instantiating.
VIvienne
using UnityEngine;
using System.Collections;
public class OrbAnimator : MonoBehaviour
{
// define prefabs
public Transform[] AnimationPrefabs;
private int PlayingCurrentAnimation;
private bool IsPlaying;
private float time = 0.0f;
// Use this for initialization
void Start ()
{
}
// Update is called once per frame
void Update ()
{
if (IsPlaying) {
time += Time.deltaTime;
if (time > 5.0f) {
Stop ();
time = 0.0f;
IsPlaying = false;
}
}
}
public void Stop ()
{
GameObject[] gameObjects;
gameObjects = GameObject.FindGameObjectsWithTag ("OrbAnimations");
for(var i = 0 ; i < gameObjects.Length ; i ++)
{
Destroy(gameObjects[i]);
}
Debug.Log ("Destroy");
time = 0.0f;
IsPlaying = false;
}
// play a animation
public void PlayAnimation (int AnimationPrefab)
{
// if animation more then length then return
if (AnimationPrefab > AnimationPrefabs.Length || AnimationPrefab < 0) {
return;
}
Stop ();
Transform prefab;
//Attach a prefab to the root
prefab = Instantiate (AnimationPrefabs [AnimationPrefab], new Vector3 (0.0f, -1.0f, 0.0f), new Quaternion (0.0f, 0.0f, 0.0f, 0.0f)) as Transform;
// Change tag
prefab.gameObject.tag = "OrbAnimations";
IsPlaying = true;
}
}