- Home /
Player regeneration
Hello i am new to programming I want my Player to regenerate Health when he steps inside a box collider i'm using these scripts
//playerHealth.js
var healthTexture : Texture2D;
var healthBorder : Texture2D;
var adjust : int;
static var health : int = 100; // static lets it be accessed by other scripts (eg: playerHealth.health = xxxxx)
function Update(){
adjust = health * 3;
}
function OnGUI () {
GUI.DrawTexture(Rect(43,Screen.height - 65,314,36), healthBorder);
GUI.BeginGroup(Rect(55,Screen.height - 55,adjust,15));
GUI.DrawTexture(Rect(0,0,290,15), healthTexture);
GUI.EndGroup();
}
The next script is my player takes damage when he steps inside a collider
//damage.js
var enter : boolean = false;
var damage = 10;
function Update () {
if(enter == true){
playerHealth.health -= damage * Time.deltaTime;
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
(enter) = false;
}
}
And this is my regeneration when inside a collider but nothing happens. It's almost the same as the damage script exept that i changed the "playerHealth.health -= damage Time.deltaTime;" to "playerHealth.health += damage Time.deltaTime;" //regeneration.js
var enter : boolean = false;
var regen = 10;
function Update () {
if(enter == true){
playerHealth.health += regen * Time.deltaTime;
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
(enter) = false;
}
}
Answer by Zodiarc · May 22, 2014 at 12:16 PM
If the playerHealth.js is attached to the player model, try the following in the regeneration.js:
var playerHealth:playerHealth;
function Start(){
playerHealth = GameObject.FindWithTag("Player").GetComponent("playerHealth");
}
// Your Update function....
Answer by revanl · May 23, 2014 at 11:21 AM
I tried what you said but it gives me an error "UnityEngine GameObject.GetComponent" for the argument list "playerHealth" was not found
var enter : boolean = false;
var regen = 10;
var playerHealth:playerHealth;
function Start(){
playerHealth = GameObject.FindWithTag("Player").GetComponent(playerHealth);
}
function Update () {
if(enter == true){
playerHealth.health += regen * Time.deltaTime;
}
}
function OnTriggerEnter (other : Collider){
if (other.gameObject.tag == "Player") {
enter = true;
}
}
function OnTriggerExit (other : Collider){
if (other.gameObject.tag == "Player") {
(enter) = false;
}
}
That was my fail.
playerHealth = GameObject.FindWithTag("Player").GetComponent("playerHealth");
should do the job. The script is simply missing a reference to playerHealth. By adding this line, you tell the script where it can find the variable.
I also updated my answer.
Your answer
Follow this Question
Related Questions
Regain health on GUI 2 Answers
How do i make my player die? 2 Answers
Player to Gui Object Communication in C# 1 Answer