- Home /
My character stats script doesn't identify my variables
My script that i set up for my character stats keeps giving me an error. I have tried multiple solutions for it but none of them have done anything. Please help. Script: ( PS i am very new to unity.) using System.Collections; using System.Collections.Generic; using UnityEngine;
public class characterStats : MonoBehaviour {
public int maxHealth = 100;
public int currentHealth { get; private set; }
public AudioClip deathSound;
public AudioClip hurtSound;
public Vector3 spawn;
public Transform[] enemies;
float distancefrom_enemies;
public float range_of_getting_attacked;
public stat damage;
public stat armor;
void OnGUI()
{
GUI.Label(new Rect(10,10,100,20), currentHealth.ToString());
GUI.Label(new Rect(10,100,100,20), armor.ToString());
}
void Awake ()
{
//setting the health to the maximum value.
currentHealth = maxHealth;
//finding enemies
GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
}
void Update ()
{
//testing for when we get damaged.
if (range_of_getting_attacked < distancefrom_enemies)
{
TakeDamage (10);
AudioSource.PlayClipAtPoint (hurtSound, transform.position);
}
Transform GetClosestEnemy = (Transform[] enemies);
{
Transform tMin = null;
float minDist = Mathf.Infinity;
Vector3 currentPos = transform.position;
foreach (Transform t in enemies)
{
float dist = Vector3.Distance(t.position, currentPos);
if (dist < minDist)
{
tMin = t;
minDist = dist;
}
}
return tMin;
}
}
public void TakeDamage (int damage)
{
//armour effects how much damage we take.
damage -= armor.getValue ();
damage = Mathf.Clamp (damage, 0, int.MaxValue);
//damages us.
currentHealth -= damage;
Debug.Log (transform.name + " takes " + damage + "damage.");
if (currentHealth <= 0)
{
Die();
//if we have no health, then we die.
}
}
//defining die:
public virtual void Die ()
{
//insert dying script here
gameObject.transform.position = spawn;
currentHealth = maxHealth;
AudioSource.PlayClipAtPoint (deathSound, transform.position);
Debug.Log (transform.name + " died.");
}
}
You will have seen my other additions. The more I looked the more I found so I deleted them.
Answer by unit_nick · Sep 12, 2017 at 08:14 AM
/*
* Here's what I think might work for you.
*
* I have updated to add armor decay
*/
using UnityEngine;
public class characterStats : MonoBehaviour
{
public int maxHealth = 100;
public int damage = 10;
public float armor = 10;
public float armorDamagePercentage = 10;
public float damageRadius = 10;
public AudioClip deathSound;
public AudioClip hurtSound;
public Vector3 spawn;
public Transform[] enemies;
private float currentHealth;
private float hurtTime = 0;
//public stat damage;
//public stat armor;
void OnGUI()
{
GUI.Label(new Rect(10, 10, 100, 20), currentHealth.ToString("F0")); // "F0" = 0 decimal places
GUI.Label(new Rect(10, 100, 100, 20), armor.ToString("F2")); // "F2" = 2 decimal places
}
void Awake()
{
//setting the health to the maximum value.
currentHealth = maxHealth;
//finding enemies
GameObject[] enemies = GameObject.FindGameObjectsWithTag("enemy");
}
void Update()
{
bool hurt = false;
Vector3 myPosition = transform.position;
foreach (Transform enemy in enemies)
{
float distance = Vector3.Distance(myPosition, enemy.position);
if (distance < damageRadius)
{
hurt = true;
// If hurt is a per enemy total
TakeDamage();
}
}
if (hurt)
{
// If hurt is a maximum total
// TakeDamage();
// let sound complete before repeating
// since I cant see it being intialized I will prevent error with check for null
float time = Time.time;
if (hurtSound != null && time - hurtTime > hurtSound.length)
{
hurtTime = time;
AudioSource.PlayClipAtPoint(hurtSound, transform.position);
}
}
}
public void TakeDamage()
{
// since damage seems to be based on proximity using delta
float deltaDamage = damage * Time.deltaTime;
float deltaArmor = armor * Time.deltaTime;
// damage player
currentHealth -= deltaDamage - deltaArmor;
// damage armor
armor = Mathf.Max(0f, armor - deltaDamage * armorDamagePercentage * 0.01f);
//if we have no health, then we die.
if (currentHealth <= 0)
Die();
}
//defining die:
public virtual void Die()
{
//insert dying script here
gameObject.transform.position = spawn;
currentHealth = maxHealth;
AudioSource.PlayClipAtPoint(deathSound, transform.position);
Debug.Log(transform.name + " died.");
}
}
Answer by tormentoarmagedoom · Sep 12, 2017 at 10:01 AM
Hello @lightyearz2 !
Just a tip:
If you declare a variable as public, and set the value in the script at same time you declare with:
int power = 3;
Then you change the value at the editor inspector to 4 , the value at the inspector will preveal. So, the value will be 4. If you now change the value at the script to 5, with
int power = 4;
But at the inspector there is still a "3", th value will be 3.
You need to declare it, and initialize separetly at the Start
void Start ()
{
power = 5;
}
This way, the script change the value after the inspector does, so the value will be 5.
Bye:D