- Home /
Question by
Nefarem · Aug 15, 2016 at 08:46 PM ·
programming
Destroying assets is not permitted to avoid data loss
I Want to create a life system, at the time when a player takes damage is deducted his life. Unfortunately, when I want to remove the heart gets the message: Destroying assets is not permitted to avoid data loss.
Code:
public int lifeValue = 3;
public Transform heartLife1;
public GameObject heart;
void Start ()
{
Instantiate(heart, heartLife1.position, heartLife1.rotation);
}
void Update () {
if (lifeValue == 2)
{
Destroy (heart);
}
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
lifeValue -= 1;
}
Comment
Answer by Landern · Aug 15, 2016 at 08:49 PM
You're killing your prefab instead of the instance that was created, you may want adjust to keep a temporary variable for your instantiated version:
public int lifeValue = 3;
public Transform heartLife1;
public GameObject heartInstance; // an instance of heart the prefab attached
public GameObject heart;
void Start ()
{
heartInstance = (GameObject)Instantiate(heart, heartLife1.position, heartLife1.rotation);
}
void Update () {
if (lifeValue == 2)
{
Destroy (heartInstance);
}
}
void OnTriggerEnter (Collider other)
{
if (other.tag == "Player")
{
lifeValue -= 1;
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220612090820im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Prior Knowledge 0 Answers
Make a game in C# 0 Answers
How to start in Unity? 1 Answer
Mecanim Jump up HELP!! 1 Answer