Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NewbieJD · Nov 01, 2017 at 05:43 PM · gameobjectgetcomponentreferencingpublic variabledefinition

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;
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
1
Best Answer

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NewbieJD · Nov 01, 2017 at 06:38 PM 0
Share

That was it, thank you so much! All working as intended now :)

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

103 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges