Hi!
I am trying to destroy the player when the player slider gets to <= 0 (currentHealth).
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class PlayerHealthSilderScript : MonoBehaviour {
public ModeManager dead;
public static float currentHealth;
public static float maxHealth;
public Slider playerHealthSlider;
// Use this for initialization
void Start () {
currentHealth = 10f;
maxHealth = 10f;
}
// Update is called once per frame
void Update () {
playerHealthSlider.value = calculateHealth();
if(currentHealth <= 0f){
dead.DestroyChild();
}
}
float calculateHealth(){
return currentHealth / maxHealth;
}
public void TakeDamage(){
currentHealth--;
}
}
I am trying to use the method DestroyChild() in my ModeManager script.
using UnityEngine;
using System.Collections;
public class ModeManager : MonoBehaviour {
public void YellowMode(){
// Instantiate PlayerYellow
DestroyChild();
GameObject playerYellow = Instantiate(Resources.Load ("PlayerYellow")) as GameObject;
playerYellow.transform.SetParent (GameObject.FindGameObjectWithTag("Player").transform, true);
}
public void RedMode(){
// Instantiate PlayerRed
DestroyChild();
GameObject playerRed = Instantiate(Resources.Load ("PlayerRed")) as GameObject;
playerRed.transform.SetParent (GameObject.FindGameObjectWithTag("Player").transform, true);
}
public void BlueMode(){
// Instantiate PlayerBlue
DestroyChild();
GameObject playerBlue = Instantiate(Resources.Load ("PlayerBlue")) as GameObject;
playerBlue.transform.SetParent (GameObject.FindGameObjectWithTag("Player").transform, true);
}
public void GreenMode(){
// Instantiate PlayerGreen
DestroyChild();
GameObject playerGreen = Instantiate(Resources.Load ("PlayerGreen")) as GameObject;
playerGreen.transform.SetParent (GameObject.FindGameObjectWithTag("Player").transform, true);
}
public void GreyMode(){
// Instantiate PlayerGrey
DestroyChild();
GameObject playerGrey = Instantiate(Resources.Load ("PlayerGrey")) as GameObject;
playerGrey.transform.SetParent (GameObject.FindGameObjectWithTag("Player").transform, true);
}
public void PurpleMode(){
// Instantiate PlayerPurple
DestroyChild();
GameObject playerPurple = Instantiate(Resources.Load ("PlayerPurple")) as GameObject;
playerPurple.transform.SetParent (GameObject.FindGameObjectWithTag("Player").transform, true);
}
public void DestroyChild(){
foreach(Transform child in transform){
Destroy (child.gameObject);
Debug.Log("Boom");
}
}
}
The method is not being called.
I tried to call a method in my PlayerController with DestroyPlayer() instead.
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
public class PlayerController : MonoBehaviour {
public GameObject bullet;
public float bulletSpeed;
void Update () {
if(Input.GetKeyDown(KeyCode.Space)){
GameObject playerBullet = Instantiate(bullet, transform.position, Quaternion.identity) as GameObject;
playerBullet.rigidbody2D.velocity = new Vector3(0,bulletSpeed,0);
playerBullet.transform.parent = transform;
}
}
public void DestroyPlayer(){
Destroy(gameObject);
}
}
But when I do that, I get the “Destroying assets is not permitted to avoid data loss.” Error.
How could I do this?
Oh and the player spawns from this
using UnityEngine;
using System.Collections;
public class CreatePlayer : MonoBehaviour {
// Use this for initialization
void Awake () {
GameObject slider = Instantiate(Resources.Load("PlayerHealthSlider")) as GameObject;
slider.transform.SetParent (GameObject.FindGameObjectWithTag("Canvas").transform, false);
GameObject player = Instantiate(Resources.Load ("PlayerYellow")) as GameObject;
player.transform.SetParent (GameObject.FindGameObjectWithTag("Player").transform, true);
}
// Update is called once per frame
void Update () {
}
}