- Home /
Does not contain definition
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;
Answer by Glurth · Nov 01, 2017 at 06:32 PM
namespace CompleteProject {
Your PlayerHealth class is defined inside this namespace.
In order to use it in your LifePickup class, you need to either, define the LifePickup class inside the same namespace, or add a "using CompleteProject" line at the top. (or reference everything using the full namespace path, eg. CompleteProject.PlayerHealth)
So, I'm surprised the compiler even get at far as the error you mentioned. I would have expected it to error-out on the declaration of the PlayerHealth member of LifePickup!
FYI: I don't see any other issues that could cause this problem.
That was it, thank you so much! All working as intended now :)
Your answer
Follow this Question
Related Questions
I have a little problem getting a component from another script 1 Answer
how to access a float from one script in other script in different objects 3 Answers
Object reference not set to an instance of an object 3 Answers
Referencing variables via external script? 1 Answer
GetComponent vs AddComponent 3 Answers