Hey. Im trying to make a Resource manager. I have a system where you can chop down trees but I want ot make it so you get wood if it is fallen. I have referenced the UI and made an int but I cant figure out how I can detect if a tree has been fallen. I will reference my Tree script (The collider has a tag for the axe to work) Code (csharp): using System.Collections; using System.Collections.Generic; using UnityEngine; public class Tree : MonoBehaviour { //Variables GameObject thisTree; public int treeHealth = 5; public bool isFallen = false; [LIST=1] [*]public void Start() [*]{ [*] thisTree = transform.parent.gameObject; [*]} [*]public void Update() [*]{ [*] if(treeHealth <= 0 && isFallen == false) [*] { [*] Rigidbody rb = thisTree.AddComponent<Rigidbody>(); [*] rb.isKinematic = false; [*] rb.useGravity = true; [*] rb.AddForce(Vector3.forward, ForceMode.Impulse); [*] StartCoroutine(destroyTree()); [*] isFallen = true; [*] } [*]} [*]public IEnumerator destroyTree() [*]{ [*] yield return new WaitForSeconds(10); [*] Destroy(thisTree); [*]} [/LIST] }
Just make the isFallen a variable of that script which other scripts can look at. `public bool isFallen;'.
You're already detecting if a tree has been fallen in line 19 of the above script. So I think your real question is, how do you update your count of trees fallen (which is basically like a score)? There are lots of ways to tackle this; if you work through a few of the tutorials, you'll see several methods. But here's what I would probably do. Define a ResourceManager class (a MonoBehaviour), which follows the standard Singleton-like pattern of having a public static instance that returns a reference to the single ResourceManager in the scene. Create an empty GameObject in the scene and attach ResourceManager to it. At this point, any script in the scene can get a reference to that thing just by using ResourceManager.instance. Give ResourceManager an AddWood() method, and of course an integer field for storing how much wood it has. In your Tree script, right around line 19 where you've detected the tree is fallen, call ResourceManager.instance.AddWood(). Also give ResourceManager a string UnityEvent (please see my tutorial on UnityEvent, and if it's still unclear how to create a string event, just ask) for when the amount of wood has changed. Invoke this event from AddWood. Finally, in the UI, hook this event up to your Text object that should display the amount of wood. Note that a string event isn't really ideal here; it should be an integer event, but then you'd need another script to convert that to a string for display. I have a standard script that does exactly that, so by this point I can just snap these things together like Lego... but for getting started, a string event is simpler and should be just fine. There are of course many variations on this pattern that would work, but this is my suggestion.
I did my best to follow what u said but I am still having trouble. The best i could get upto in my code is this and im not sure where I went wrong. (Im still learning) Code (csharp): using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class ResourceManager : MonoBehaviour { public Canvas canvas; public Text wood; public int Wood; public Text stone; public int Stone; // Use this for initialization void Start () { } // Update is called once per frame void Update () { } public void AddWood() { } }
You didn't setup the singleton. Code (csharp): public static ResourceManager instance; void Awake() { if(instance == null) instance = this; else if(instance != this) Destroy(gameObject); } and there's no code in AddWood() ? You maybe want something like... Code (csharp): void AddWood(int cnt){ wood += cnt; woodText.text = wood.ToString(); // yes, I changed the variable name, but you can use whatever you prefer. } Then, you'd call: Code (csharp): // wherever you're collectin/chopping down. ResourceManager.instance.AddWood(2); // adds 2 wood to your score or what not. I didn't include an event example. Event is very useful once you have more than 1 (or maybe 2) separate places you want to update. I mean, it's never bad with 1.. but.. anyways -- helps more if you have more to "notify" imho.