K
K
kontay2022-04-05 15:37:09
Unity
kontay, 2022-04-05 15:37:09

Accessing the value of an int variable from another script?

I wanted to make shooting for the character, and add such a hot bar in which the "int value" value is stored and displayed, and it is necessary that when shooting (the function of which is in the "Gun" script) in another script ("ProgressBarComponent"), this same value is reduced. Please help! (in the script there are my attempts to do this)

script ProgressBarComponent :

using UnityEngine;
using UnityEngine.UI;
using System.Collections;

    public class ProgressBarComponent : MonoBehaviour
    {
        [Header("UI Elements")] 
        [SerializeField] private Image image;

        [Header("Properties")] 
        public int value = 0;
        private int maxValue = 50;
        private bool isCorrectlyConfigured = false;

        public Transform shotpos;
        public GameObject Bullet;

        private void Start()
        {
            StartCoroutine(addScore());
        }

        private void Awake()
        {
            if (image.type == Image.Type.Filled & image.fillMethod == Image.FillMethod.Horizontal)
            {
                isCorrectlyConfigured = true;
            }
            else
            {
                Debug.Log("{GameLog} => [ProgressBarController] - (<color=red>Error</color>) -> Components Parameters Are Incorrectly Configured! \n" +
                          "Required Type: Filled \n" +
                          "Required FillMethod: Horizontal");
            }
        }

        private void LateUpdate()
        {
            if (!isCorrectlyConfigured) return;
            image.fillAmount = (float) value / maxValue;
        }




        public void SetValue(int value) => this.value = value;
        public void SetMaxValue(int maxValue) => this.maxValue = maxValue;

        private IEnumerator addScore()
        {
            while (true)
            {
                yield return new WaitForSeconds(1);
                if (value < 50) 
                {
                    value++;
                }

            }
        }


gun script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour
{
    public float offset;
    public Transform shotpos;
    public GameObject Bullet;

    void Update()
    {
        Vector3 difference = Camera.main.ScreenToWorldPoint(Input.mousePosition) - transform.position;
        float rotZ = Mathf.Atan2(difference.y, difference.x) * Mathf.Rad2Deg;
        transform.rotation = Quaternion.Euler(0f, 0f, rotZ + offset);
    }

    public void Fire(int value)
    {
        if (Input.GetMouseButton(0) && value > 4)
        {
            Instantiate(Bullet, shotpos.position, transform.rotation);
            value -= 5;
        }
    }
        
}


Discord for communication: kontay#7833

Answer the question

In order to leave comments, you need to log in

2 answer(s)
F
freeExec, 2022-04-05
@freeExec

Add a link to the progressbar in the gun and you will have access.
wallet for payment: 792753214756

M
Morrowind, 2022-04-05
@hekkaaa

Hey!
There are 3 options, as I see it, and freeExec told you correctly - you need a link to the value field (it can even be made a property). 1. You can call the shooting method and throw the value
argument with the ref parameter into its arguments . Example: Thus, inside the method, do value-- or value++. 2. You can make value static and throw it in the same way as in option 1, only without ref 3. Throw the ENTIRE ProgressBarComponent object
and access its public properties or fields by changing them inside the shot method. (Well, so-so way in my opinion). You can do this through DI, for example, if it is used in game development, of course.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question