I'm having a null reference error, but everything exists in that line of code :(

NullReferenceException: Object reference not set to an instance of an object
FruitTree.Update () (at Assets/Scripts/FruitTree.cs:29)

heres my error and below are my scripts, could someone please help me

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

public class FruitTree : MonoBehaviour
{
    GameObject parentSoil;

    public Soil soilScript;

    public bool apple;
   
    public Vector3 originalPos;

    public Transform[] appleSpawns;

    public bool grown;

    public GameObject apples;
    // Start is called before the first frame update
    void Start()
    {
        originalPos = transform.position;
    }

    // Update is called once per frame
    void Update()
    {
        if(apple == true && soilScript.timesWatered == 5 && grown == false)
        {
            GetComponent<Animator>().SetBool("Grown", true);

            transform.position = new Vector3(originalPos.x, originalPos.y + 1.25f, 0);

            foreach(Transform applespawn in appleSpawns)
            {
                Instantiate(apples, applespawn.position, applespawn.rotation);
            }

            grown = true;
        }
    }

    void OnTriggerEnter2D(Collider2D other)
    {
        if(parentSoil == null)
        {
            parentSoil = other.gameObject;

            Debug.Log("Parent Soil: " + parentSoil.name);

            soilScript = parentSoil.GetComponent<Soil>();
        }
       
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Soil : MonoBehaviour
{
    public bool watered;

    public Color dark;

    Player playerScript;

    public GameObject outline;

    public Color unwatered;

    GameObject childPlant;

    public int timesWatered;
    // Start is called before the first frame update
    void Start()
    {
       
    }

    // Update is called once per frame
    void Update()
    {
        playerScript = GameObject.Find("Player").GetComponent<Player>();

        if(playerScript.soil == gameObject)
        {
            outline.SetActive(true);
        }
        else
        {
            outline.SetActive(false);
        }

        if(childPlant == null)
        {
            timesWatered = 0;
        }
    }

    public IEnumerator Watering()
    {
        yield return new WaitForSeconds(3);

        watered = true;

        gameObject.GetComponent<SpriteRenderer>().color = dark;

        playerScript.doingAction = false;

        StartCoroutine(Drying());

        timesWatered++;
    }

    public IEnumerator Planting(GameObject plant)
    {
        yield return new WaitForSeconds(.5f);

        Instantiate(plant, transform.position + new Vector3(0, .25f, 0), transform.rotation);

        playerScript.doingAction = false;

        childPlant = plant;
    }

    IEnumerator Drying()
    {
        while(gameObject == gameObject)
        {
            yield return new WaitForSeconds(15);

            watered = false;

            gameObject.GetComponent<SpriteRenderer>().color = unwatered;
        }
    }
}

also if you have any suggestion for my code, please feel free to suggest away :smile:

short answer → prove it.

Show debugging showing all those things exist 1 line before it says it doesnt

wait so what should i do to fix it?

Well the nullreference wont lie, so there is your answer… set it to the right thing

but what’s missing in my script? ive checked multiple times and everything seems to exist on line 29

Thats why I said prove it, debug it, you will find out whats not there or right, and then you can fix it.

I figured the issue out, so there was nothing wrong with the code, all that happened was that it would do the update function before the trigger function and wouldnt assign the “soilScript” variable in time

Wrong conclusion. The Update function does what the Unity engine expects. You’re looking for something that doesn’t exist at the moment or cannot be found. You have the option to protect yourself from such situations by checking if something has been found (is not null), or preferably, not searching for elements during code execution at all. Avoid using all Get and Find methods; instead, assign references simply by making public fields or private fields with the [SerializeField] attribute. Then, you just grab and drop them in the editor, creating the reference before even one frame of screen refresh occurs.

1 Like

It seems you have misunderstood NullReferenceException (NRE).

NRE is a RUNTIME error, not a compiler error.

That means there may be nothing at all missing in your script.

How to fix a NullReferenceException error

https://forum.unity.com/threads/how-to-fix-a-nullreferenceexception-error.1230297/

Three steps to success:

  • Identify what is null ← any other action taken before this step is WASTED TIME
  • Identify why it is null
  • Fix that