I dont need it anymore
My C# Script not working
Hello. So recently I have been trying to make an Island Survival game. Unfortunately I cant seem to get my Wood counter to work. Here are my scripts.
1. Tree Fall Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Tree : MonoBehaviour {
//Variables
GameObject thisTree;
public int treehealth = 5;
private int woodCount;
public bool isFallen = false;
void Awake()
{
}
private void Start()
{
thisTree = transform.parent.gameObject;
}
private 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;
//Counter
}
}
private IEnumerator destroyTree()
{
yield return new WaitForSeconds(10);
Destroy(thisTree);
}
}
2. Material Counter
using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;
public class MaterialCounter : MonoBehaviour {
public int woodCount = 0;
Tree healthtree;
public Text woodText;
// Use this for initialization
void Start () {
setWoodText();
}
// Update is called once per frame
void Update () {
if(healthtree.isFallen == true)
{
woodCount = woodCount + 1;
setWoodText();
}
}
void setWoodText()
{
woodText.text = "Wood: " + woodCount.ToString();
}
}
My goal for this is to get a Wood Counter at the top left showing how much "Wood" I have in my inventory. I am pretty new to Unity and C# and have been trying to learn the basics. I would love if someone can show me the right script and preferably explain it a bit on the reasoning behind it. PS: When I run the script this is what it says. "NullReferenceException: Object reference not set to an instance of an object MaterialCounter.Update () (at Assets/Scripts/MaterialCounter.cs:23) "
Answer by LilGames · Aug 13, 2017 at 10:03 PM
What object is MaterialCounter script on?
It sounds like you have not assigned a Tree object to the MaterialCounter. Make Tree public:
public Tree healthtree;
Or actually I think it will have to be a GameObject:
public GameObject healthtree;
And then in the inspector, assign the tree gameobject to that to create a reference. It's the same concept that you already did in order to access "woodText".
The $$anonymous$$aterialCounter script is on the Character. And what I am trying to get is my script to add maybe like 1 Wood every time I break a tree. The problem when I do "public Tree healthtree" is that I have many trees. Also when I did this my Wood Counter was not affected when I destroyed the tree that put in my "public Tree healthtree"
Answer by Komayo · Aug 14, 2017 at 12:22 AM
For the first issue, you can create a prefab of the tree object, already with script included, that way all the trees you put in place have the script already attached. Dunno what system you are using, you can also hardcode to include the script in all the trees found in scene.
For the second issue, add a OnDestroy() function and update the counter there. It should then update after destroying the object.
For the 2nd part of your idea. Do you have any basic example script I can see. Sorry for the constant questions its just I am pretty new and I have been having a hard time with this basic stuff. Thanks By the way!
void OnDestroy()
{
woodCount--; // equals woodCount = woodCount - 1;
woodText.text = "Wood: " + woodCount.ToString();
}
Follow this Question
Related Questions
How do I trigger a game object to move a desired distance at a desired speed? 1 Answer
How to change button.spriteState.pressedSprite sprite by script - Unity 4.7.1 1 Answer
Using multiple materials on a 2d mesh 0 Answers
Problem with c# script 0 Answers
ObjectParameter Usage in a Custom Skybox 0 Answers