Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice
  4. Dismiss Notice

Question How can I make one int equal an int from another script?

Discussion in 'Scripting' started by MASTERAFF0, Sep 20, 2021.

  1. MASTERAFF0

    MASTERAFF0

    Joined:
    Aug 3, 2020
    Posts:
    5
    I want to make my character's health be determined by how many of the currency they have, kind of like rings in sonic games (If you have one ring or more, you survive a hit; if less than one, you die). How do I do that here:



    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5.  
    6. public class GameManager : MonoBehaviour
    7. {
    8.     public int moneyAmount;
    9.    
    10.     public void AddMoney(int moneyToAdd)
    11.     {
    12.         moneyAmount += moneyToAdd;
    13.     }
    14. }

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class HealthManager : MonoBehaviour
    6. {
    7.     public int currentMoney;
    8.     public int maxHealth;
    9.     public int currentHealth;
    10.     // Start is called before the first frame update
    11.     void Start()
    12.     {
    13.         currentMoney = 0;
    14.         currentHealth = 1;
    15.     }
    16.  
    17.     // Update is called once per frame
    18.     void Update()
    19.     {
    20.         //currentMoney = [moneyAmount in GameManager script]
    21.         if (currentRings > 0)
    22.         {
    23.             currentHealth = 2;
    24.         }
    25.         else
    26.         {
    27.             currentHealth = 1;
    28.         }
    29.     }
    30. }
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,724
    Best practice would be not to have multiple copies of the same information that you need to keep in sync. Instead, where you need to use that value, keep a reference to the owner of the value and read it directly from there. This concept is called "single source of truth" https://en.wikipedia.org/wiki/Single_source_of_truth

    It keeps you from writing a bunch of error prone code to keep more than one variable in sync unnecessarily.

    For example, in HealthManager, you should simply have a reference to your GameManager and read the current amount of money directly from there, instead of creating a copy of the same value in HealthManager that you would need to keep in sync all the time.

    As for how to get a reference to your GameManager from your HealthManager, this video explains several excellent ways to do so:
     
  3. MASTERAFF0

    MASTERAFF0

    Joined:
    Aug 3, 2020
    Posts:
    5
    Thank you, I managed to make it work!