Hi all, I’m currently creating a modified version of the Survival Shooter demo. I’ve added various elements including power-ups and lives but have run into an issue. I am trying to reference a public integer (lives) that I have created in another script. Lives resides in the ‘PlayerHealth’ script and I want the ‘LifePickup’ script to be able to increase this integer. However, when I try and reference the lives integer I am getting the message ‘Error CS0117: ‘PlayerHealth’ does not contain a definition for ‘lives’.’ I am able to reference other integers in the PlayerHealth script that are established in exactly the same way but not my lives integer. I’ve trawled the forums but can’t find any solution to this.
The first code snippet attached is two exerts from ‘PlayerHealth’, this demonstrates how I have set up all the variables. I can add in further code from this class if needed. The second is the ‘LifePickup’ script which is trying to reference the component. This is being done on the final line.
Any help that I can get would be really appreciated. Thanks!
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using UnityEngine.SceneManagement;
namespace CompleteProject
{
public class PlayerHealth : MonoBehaviour
{
public int startingHealth = 100;
public int currentHealth;
public Slider healthSlider;
public Image damageImage;
public AudioClip deathClip;
public float flashSpeed = 5f;
public Color flashColour = new Color(1f, 0f, 0f, 0.1f);
public int startingLives = 3;
public int lives;
Animator anim;
AudioSource playerAudio;
PlayerMovement playerMovement;
PlayerShooting playerShooting;
bool isDead;
bool damaged;
void Awake ()
{
anim = GetComponent <Animator> ();
playerAudio = GetComponent <AudioSource> ();
playerMovement = GetComponent <PlayerMovement> ();
playerShooting = GetComponentInChildren <PlayerShooting> ();
currentHealth = startingHealth;
lives = startingLives;
Livestext.livesnow = lives;
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class LifePickup : MonoBehaviour {
private PlayerHealth playerHealth;
private Renderer rend;
private BoxCollider coll;
private GameObject player;
void Start()
{
rend = GetComponent<Renderer> ();
coll = GetComponent<BoxCollider> ();
player = GameObject.FindGameObjectWithTag ("Player");
playerHealth = player.GetComponent <PlayerHealth> ();
}
void OnTriggerEnter(Collider other)
{
if (other.gameObject.CompareTag ("Player"))
{
AudioSource audio = GetComponent<AudioSource> ();
audio.Play ();
rend.enabled = false;
coll.enabled = false;
playerHealth.lives += 1;