HEY, HOW’S IT GOING, BUDDYS?
I need some your help today. All in all, I have 3 main scripts: globalvariables.cs (where I save all my global variables), onTreeClicked.cs (event when I click on button) and CoinExchange.cs (also event when I click on button). All other scripts doesn’t really have a matter.
I’ll try explain what they do. In my first script (globalvariables.cs) I save all my global varibles:
globalvaribles.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class globalvariables : MonoBehaviour {
public int Wood; // Creating a Wood variable
public int Coin; // Creating a Coin variable
}
Then I take them to my second script (onTreeClicked.cs). I’mma left screenshot with comments what every line do.
I add 1 to Wood a click. And as I can understand it changes it’s value from default. (in my globalvariables I dont say that Wood = 0 or smth. So I reckon that when I add 5 to Wood it equals 5.)
onTreeClicked.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class onTreeClicked : globalvariables { // Getting all variables from globalvariables script
private Shake shake; // Variable for Camera Shaking
private Hit hit; // Varible for hit
private ParticleSystem woodeffect; // Wood icon appears above the tree
private ParticleSystem pluseffect; // 'plus effect' next to wood icon
private Text woodcount; // Wood count text
private Text coincount; // Coin count text
void Start () {
shake = GameObject.FindGameObjectWithTag("ScreenShake").GetComponent<Shake>(); // Getting Components//
hit = GameObject.FindGameObjectWithTag("Player").GetComponent<Hit>(); //*******************//
woodeffect = GameObject.FindGameObjectWithTag("woodeffect").GetComponent<ParticleSystem>(); //*******************//
pluseffect = GameObject.FindGameObjectWithTag("pluseffect").GetComponent<ParticleSystem>(); //*******************//
woodcount = GameObject.FindGameObjectWithTag("woodcount").GetComponent<Text>(); //*******************//
coincount = GameObject.FindGameObjectWithTag("coincount").GetComponent<Text>(); // Getting Components//
woodcount.text = Wood.ToString(); // Display start Wood value
coincount.text = Coin.ToString(); // Display start Coin value
pluseffect.Stop(); // Stoping plus effect
woodeffect.Stop(); // Stoping wood effect
}
public void OnButtonTapped() // onClick function
{
shake.camShake(); // Shake animation activated
hit.charHit(); // Hit animation activated
Wood++; // Add 1 to Wood variable
woodcount.text = Wood.ToString(); // Show new value of wood
woodeffect.Play(); // Playing wood effect
pluseffect.Play(); // Playing plus effect
}
void Update () {
}
}
In my third script (CoinExchange) I wanna exchange wood for coins (1 coin = 5 wood). But when I tryna get Wood varible from onTreeClicked.cs (that must equal some value) I get 0. I mean, when I’m checking Wood variable it equals 0.
CoinExchange.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CoinExchange : onTreeClicked { // Getting variables from onTreeClicked.cs
private Text coincount; // Coin count text
void Start (){
coincount = GameObject.FindGameObjectWithTag("coincount").GetComponent<Text>(); // Getting Components//
}
void Update (){
}
public void Exchange() // onClick event
{
Debug.Log(Wood); // Checking Wood value
}
}