OK, so i have a complex script. A money script to be exact. It uses moneytext for its text. I cannot figure out how to link those 2 to anthoer script then use them then send it back to my other script. Would this be possible if so how? I am having some trouble and i understand a tiny amount of it
Money Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MoneyManager : MonoBehaviour {
public BuySystem Guns;
public static int Money; // Makes money value accessible to more than one script
public Text moneyText; // Adds a text for money
void Start() // Teells the system for everything in brackets to start
{
Money = 0;
moneyText.text = Money.ToString(); // Tells the text to refresh it with the Money number
}
void Update() //Makes sure to constantly check values
{
if (Input.GetButtonDown("Fire1")) // If player presses left-click, Adds money
{
Money += 100;
moneyText.text = Money.ToString(); // Tells the text to refresh it with the Money number
}
if (Input.GetButtonDown("Fire2")) // If player presses right-click, Subtracts money
{
Money -= 1; // Money to Subtract
if (Money < 0) // Makes Sure number cant go lower than -1
{
Money = 0; // If money is less than 0 replaces it with 0
}
if (Money < -1 ) // Makes Sure number cant go lower tahn 0
{
Money = 0; // If money is lower than 0 replaces it with 0
}
moneyText.text = Money.ToString(); // Tells the text to refresh it with the Money number
}
}
// TODO : CONNECT THIS SCRIPT WITH SHOPCOLLIDER WHEN I WAKE UP, also set gun prices.
}
I cant figure out how to link Money. and Moneytext to this other script then that other scripts uses MoneyText and Money fore itself. The one problem i have is that when i make MoneyText static it removes it from the editor. The UI text i have is called “Money Text” from the canvas.
Buy Script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
[System.Serializable]
public class BuySystem
{
public GameObject Gun;
public int Cost;
public static Text moneyText;
void Start ()
{
moneyText.text = MoneyManager.Money.ToString();
}
void Update ()
{
if (Input.GetKeyDown("p"))
{
if (MoneyManager.Money >= Cost)
{
MoneyManager.Money -= Cost;
Debug.Log("PurcaseComplete");
moneyText.text = MoneyManager.Money.ToString();
}
}
if (Input.GetKeyDown("C"))
{
MoneyManager.Money += 10;
moneyText.text = MoneyManager.Money.ToString();
}
}
// TODO : FIND OUT HOW TO USE C# VARIABLES GLOBALLY
}