• 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 saundersj5339 · Nov 24, 2019 at 06:49 PM · variableshealthbar

,Keeping a variable between scripts constant?

I am having trouble keeping a variable between 2 scripts the same. I have one deriving from the other, yet one of the variables is treated as a different int.

First one

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class Warmth : MonoBehaviour
 
     //Warmth Script, first one
 
 {
     public int heatLvl; // = 100f;
     public string sceneName;
 
     void Start()
     {
         InvokeRepeating("decreaseHeat", .25f, .25f);
     }
 
     void decreaseHeat()
     {
         if (heatLvl > 0)
         {
             heatLvl -= 1;
         }
     }
     void FixedUpdate()
     {
         Debug.Log(heatLvl);
 
         if (heatLvl <= 0)
         {
             SceneManager.LoadScene(sceneName);
         }
 
     }
 
     private void OnTriggerEnter2D(Collider2D other)
     {
         heatLvl = 100;
     }
 
 }


second one

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.UI;
 
 public class Health : Warmth {
 
     //Health script, second one
 
     private const float MAX_HEALTH = 100f;
 
     public Warmth warmth;
 
     public float health = MAX_HEALTH;
 
     private Image healthBar;
 
     private void Start()
     {
         healthBar = GetComponent<Image>();
         InvokeRepeating("decreaseHeat", .25f, .25f);
     }
 
     private void FixedUpdate()
     {
         healthBar.fillAmount = health / MAX_HEALTH;
         health = heatLvl;
     }
 
 }
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
0

Answer by Ermiq · Nov 25, 2019 at 10:53 AM

Let me guess. You've attached both of these to the same game object probably.


The thing is, you don't understand what is the inheritance and where and how it should be used. Yes, inheritance is a good thing when you want some similar classes to have mutual methods, but you can't have both the base class and its ancestor at the same object. When you do this, you actually get one Health class which is also the Warmth and you have another Warmth which is just warmth.


By making a class inherit from another class you actually make it both the Health and the Warmth at the same time, and when you initialize a Health component you automatically initialize an instance that has its own Warmth variables independently of the other Warmth component.


Inheritance is a cool feature but you have to understand what is it actually and how does it work. Read some books about object oriented programming, there are awesome ones written specifically for novices, e.g. "C# 7.0 All-in-one for dummies" by John Paul Mueller was one of the books I've read when I learned programming, it's very very good book.

Comment
Add comment · 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

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

117 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 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

Help w/variables 0 Answers

How to make an healthbar 1 Answer

Draw orthographic visuals (e.g. health bar) in perspective scene? 4 Answers

Show changes in healthbar 1 Answer

HealthBar with ColorChange 1 Answer


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