ok so script is supposed to call a method from my GameManager script to increase my players health and then print the Debug.Log saying something. The Debug.Log prints but the health variable isn’t updated and I have no idea why. By the way I’m coding in C#
any help would be greatly appreciated
Heart Script:
using UnityEngine;
using System.Collections;
public class heart : MonoBehaviour {
//here is a private object that we'll set as the player in start
private GameObject target;
public GameManager gameManager;
void Start () {
//once this script starts (when a heart exists) we find the player so we can have it move towards the player
target = GameObject.Find("Player");
//here we add a bit of force at the beginning to help give the heart a nice little animation once it is spawned
rigidbody.AddForce(Random.Range(-600,600),0,Random.Range(-600,600));
}
void Update () {
//before we do anything, we just check to see if the target is not null. this helps avoid any unwanted errors
if(target != null){
//here we check the square distance so we can see how close the player is to the object.
var sqrDistance = (target.transform.position - transform.position).sqrMagnitude;
//if the square distance is less than 16, we'll let the heart do stuff.
if (sqrDistance <= 16){
//before we can get the heart to move towards the player, we want to find the direction
var dir = target.transform.position - transform.position;
dir = dir.normalized;
//now we apply force towards the player using the variable dir (aka direction).
rigidbody.AddForce(dir * 1500 * Time.deltaTime);
}
}
}
void OnTriggerEnter (Collider other) {
//if the tag of the trigger is in fact heart, we do stuff.
if(other.gameObject.tag =="Player")
{
gameManager.IncreaseHealth();
Debug.Log("heart collided from heart script");
Destroy(gameObject);
}
}
}
GameManager script:
using UnityEngine;
using System.Collections;
public class GameManager : MonoBehaviour {
//reference to controller2d script
public Controller controller;
//players life
public Texture playersHealthTexture;
//control screen positon
public float screenPositionX;
public float screenPositionY;
//control icon size on screen
public int iconSizeX = 25;
public int iconSizeY = 25;
//base lives
int playersHealth = 3;
public int maxHealth = 3;
//money
public int gems = 0;
//attack
public int playerAttack = 1;
//xp stats;
public int curEXP = 0;
public int maxEXP = 50;
public int level = 1;
bool playerStats = false;
public GUIText statsDisplay;
void Update ()
{
//level system
if(curEXP >= maxEXP){
levelUp();
}
if(Input.GetKeyDown(KeyCode.C)){
playerStats = !playerStats;
}
if(playerStats){
statsDisplay.text = "Level: " + level + " XP: " + curEXP + " / " + maxEXP;
}
else
{
statsDisplay.text = "";
}
}
//level up system
void levelUp(){
Debug.Log("level up");
curEXP = 0;
maxEXP = maxEXP + 50;
level++;
//add stats
maxHealth++;
playersHealth = maxHealth;
}
void OnGUI()
{
//control players health textures
for(int h = 0; h < playersHealth; h++)
{
GUI.DrawTexture(new Rect(screenPositionX + (h * iconSizeX), screenPositionY, iconSizeX,iconSizeY), playersHealthTexture, ScaleMode.ScaleToFit, true, 0);
}
}
void PlayerDamaged(int damage){
Debug.Log("PlayerDamaged");
if(playersHealth > 0)
{
playersHealth -= damage;
}
if(playersHealth <= 0)
{
playersHealth = 0;
RestartScene();
}
}
public void IncreaseHealth()
{
playersHealth = playersHealth + 1;
Debug.Log("increase health");
}
public void IncreaseXP()
{
curEXP = curEXP + 50;
Debug.Log("increase xp");
}
public void IncreaseGems()
{
gems++;
Debug.Log("increase gems");
}
void RestartScene(){
Application.LoadLevel(Application.loadedLevel);
}
}