Question by
lux-trails · Jan 08, 2017 at 03:35 PM ·
instantiateprefabdestroyclone
destroy asset is not permitted to avoid data loss?
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Reset : MonoBehaviour {
public static bool reset = false;
public Transform[] startingLocation;
public GameObject[] whatToSpawnPrefab;
public List<GameObject> whatToSpawnClone = new List<GameObject>();
public GameObject Player;
public Rigidbody2D P1;
public Text Sc;
public void RestartButtonClicked()
{
// reset
reset = true;
//reset score
ScoreManager.Score = 0;
//reset player
P1.velocity = Vector2.zero;
P1.angularVelocity = 0;
Player.transform.position = new Vector3 (0f, 0.4f, 0);
Player.transform.eulerAngles = Vector3.zero;
//reset stars
if (whatToSpawnClone.Count > 0)
{
for (int i = 0; i < whatToSpawnClone.Count; i++)
{
if (whatToSpawnClone[i] != null)
{
Destroy(whatToSpawnClone[i]);
}
}
}
whatToSpawnClone.Clear();
SpawnObject(0, 0, Quaternion.Euler(0.505f,0.417f,0));
SpawnObject(1, 1, Quaternion.Euler(-0.497f,0.417f,0));
SpawnObject(2, 2, Quaternion.Euler(0.007f,0.815f,0));
SpawnObject(3, 3, Quaternion.Euler(0.007f,0.017f,0));
reset = false;
}
void SpawnObject (int prefabIndex, int locationIndex, Quaternion quaternion)
{
if (prefabIndex < whatToSpawnPrefab.Length)
{
if (locationIndex < startingLocation.Length)
{
whatToSpawnClone.Add(Instantiate(whatToSpawnPrefab[prefabIndex], startingLocation[locationIndex].transform.position, quaternion) as GameObject);
}
else
{
Debug.LogError("Starting location index ( " + locationIndex + " ) need to be less than " + startingLocation.Length);
}
}
else
{
Debug.LogError("Spawn prefab index ( " + prefabIndex + " ) need to be less than " + whatToSpawnPrefab.Length);
}
}
// Update is called once per frame
void Update ()
{
}
}
Comment
i seem to be having an error on line 35 about destroyed asset not permitted to avoid data lose,
also on line 53 there is an error about accessing a destroyed gameobject when one of the runtime gameobject has been destroyed and the reset has been clicked.
@TBruce this is the repost thanks again