Answer the question
In order to leave comments, you need to log in
How to make shooting at a certain value of the int variable in another script?
In general, I just started my journey in creating games, making a platformer, and I have such a problem. There is a hot bar, it can store a number from 0 to 50, and with the value "variable int" < 10, it is possible to make a shot ... This is what I wanted to do, but I can't find how to find out the value of the int variable from another script.
hot bar code
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
namespace N.Fridman.ProgressBar.Scripts
{
public class ProgressBarComponent : MonoBehaviour
{
[Header("UI Elements")]
[SerializeField] private Image image;
[Header("Properties")]
[SerializeField] private int value = 0;
[SerializeField] private int maxValue = 50;
[SerializeField] private bool isCorrectlyConfigured = false;
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++;
}
}
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Weapon : MonoBehaviour
{
public Transform shotpos;
public GameObject Bullet;
void Update()
{
if(Input.GetKeyDown(KeyCode.F))
{
Instantiate(Bullet, shotpos.transform.position, transform.rotation);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question