NullReferenceException: Object reference not set to an instance of an object Collectt.Update ()

I do not understand a problem, NullReferenceException: Object reference not set to an instance of an object Collectt.Update ()
(My GameObject ‘Log’ is tagged with ‘Log’

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Collectt : MonoBehaviour {

    public int Health = 100;
    public GameObject log;

    void Start () {
        Resources resource = GetComponent<Resources>();

    }
   
    void Update () {
        Resources resource = GetComponent<Resources>();
        resource.log++;
    }

    public void OnTriggerEnter(Collider other)
    {
        if (other.gameObject.CompareTag("Log"))
        {
            Resources resource = GetComponent<Resources>();
            resource.log = resource.log + 1;
            Destroy(log);
        }
    }
}

That was my resource script. Below is the 'source I am getting this ‘log’ integer from.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Resources : MonoBehaviour {

    public int log;
    public int stone;
    public int Health;

    void Start () {
       
    }

Would you please give us some context. What is the intention this code is meant to fulfill?

Also, would you please clean up the code in the posts, so it’s easier for people to read?

Apparently, GetComponent cannot find such component on the game object.
Also, the line in Start is useless and the frequent call to GetComponent in Update can be improved by caching the component once.

1 Like