This is using the Unity Engine.
I have 3 scripts related to this problem. One is attached to a temporary game object that when the player collides with it, sends a message to the player Damage Controller. The player Damage Controller updates the health inside of the player’s Damage Controller script. Using Debug.Log, I can see that the player health updates as expected.
The third script is a component of a GUI Texture. From inside of the GUITexture script, I try to GetComponent of the Damage Controller.health. But running a Debug inside of the GUITexture script, for some reason the GetComponent.health is not updating as expected and is why my health status GUI Texture is not changing inside the game scene. Any help will be great.
using UnityEngine;
using System.Collections;
public class Trap: MonoBehaviour
{
//Trap intrinsic properties
public int damage = 1;
void OnTriggerEnter(Collider other)
{
if(other.tag == "Player" || other.tag == "Enemy")
{
//Debug.Log("Collision with Player Object!");
other.collider.SendMessage("ApplyDamage", damage, SendMessageOptions.DontRequireReceiver);
//this.SendMessage("SetGUITexture");
Destroy(gameObject);
}
}
}
using UnityEngine;
using System.Collections;
public class DamageController : MonoBehaviour
{
public int health = 4;
public void ApplyDamage(int damage)
{
health -= damage;
Debug.Log("Updated Health is: "+health);
if(health == 0)
{
Destroy(gameObject);
}
}
}
using UnityEngine;
using System.Collections;
public class HealthGUI : DamageController
//public class HealthGUI : MonoBehaviour
{
public int healthGUI = 4;
public DamageController damageController;
private GameObject playerGameObject;
// Use this for initialization
void Start ()
{
playerGameObject = GameObject.FindGameObjectWithTag("Player");
}
// Update is called once per frame
void Update ()
{
DamageController damageController = playerGameObject.GetComponent<DamageController>();
healthGUI = damageController.health;
Debug.Log("Health is : "+healthGUI);
}
}
Firstly I don't see a reason for that many
– Benproductions1/and you saying when a script ends... I think that's pretty clear just from the formatting... Secondly, do you get any errors? If so you should post them.No, no errors. Runs fine in game mode. But as I mentioned in the top of the question, the Debug.Log for the healthGUI = damageController.health only returns the initialized value of 4 without reflecting changes to changes in health when damage is applied. But I know for a fact that the health variable in DamageController IS being updated when the trap object sends the message to "ApplyDamage" function inside of DamageController.
– Rick.MirandaDefinitely worth removing all the Start() and Update()s that are blank just cluttering. In the last script you already declared public DamageController damageController so you should remove type "DamageController" from the call in Update(). You could also combine the damageController call to damageConroller = GameObject.FindGameObjectWithTag("Player").GetComponent<DamageController>(); Finally, that shouldn't be in Update() move it to Start(). Not sure this will solve the issues, but a little clean-up shouldn't hurt.
– getyour411Please remove those empty update and start calls in the code you've posted here to make it easier to read.
– ShrandisNo problem Shrandis. I've taken out unused Start and Update calls. Thank you.
– Rick.Miranda