I have no idea what is wrong.
Please help meh.
using UnityEngine;
using System.Collections;
using System;
public class PlayerHealth : MonoBehaviour {
private int Playerhealth = 3; // This string holds the players current health value.
private int DmgCD = 0; // This string detects if the player can currently recieve damage
public Sprite[] Healths;
public GameObject healthbar = new GameObject();
void Start () {
//healthbar.AddComponent();
Healths = Resources.LoadAll("Healths");
}
void Update()
{
if (Playerhealth == 3)
{
healthbar.GetComponent().sprite = Healths[3];
}
if (Playerhealth == 2)
{
healthbar.GetComponent().sprite = Healths[2];
}
if (Playerhealth == 1)
{
healthbar.GetComponent().sprite = Healths[1];
}
if (Playerhealth == 0)
{
healthbar.GetComponent().sprite = Healths[0];
Playerhealth = 3;
//Application.LoadLevel("Lose"); (Remove the comments from this when the lose level is finished)
}
}
private IEnumerator HealthTracker()
{
while (true)
{
yield return new WaitForSeconds(2f);
StopCoroutine(HealthTracker());
}
}
void OnCollisionEnter2D(Collision2D col) // When getting hit by a projectile with the enemy tag
{
if (col.gameObject.tag == "Missile")
{
print("damage recieved");
if (DmgCD == 0) //If the player is allowed to be damaged
DmgCD = 1; // Make it so the player can no longer recieve new damage
Playerhealth = Playerhealth - 1;
StartCoroutine(HealthTracker());
}
}
}
**The script is attempting to change a health bar depending on the players health values. The script also controls the health values and collisions.**
**Error:**
*Internal_CreateGameObject is not allowed to be called from a MonoBehaviour constructor (or instance field initializer), call it in Awake or Start instead. Called from MonoBehaviour 'PlayerHealth' on game object 'healthbar'.*
↧