Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
This question was closed Sep 23, 2017 at 10:48 AM by Agent27765 for the following reason:

I dont need it anymore

avatar image
0
Question by Agent27765 · Aug 13, 2017 at 07:07 PM · scripting problem

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) "

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

  • Sort: 
avatar image
0

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".

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Agent27765 · Aug 13, 2017 at 11:55 PM 0
Share

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"

avatar image
0

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.

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Agent27765 · Aug 14, 2017 at 12:31 AM 0
Share

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!

avatar image Komayo Agent27765 · Aug 14, 2017 at 07:30 PM 0
Share
 void OnDestroy()
 {
  woodCount--; // equals woodCount = woodCount - 1;
  woodText.text = "Wood: " + woodCount.ToString();
 }
avatar image Komayo Agent27765 · Aug 15, 2017 at 10:50 PM 0
Share

Where are you with this problem fix?

Follow this Question

Answers Answers and Comments

112 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges