Search Unity

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

Discussion in 'Scripting' started by otterkid, Feb 11, 2024.

  1. otterkid

    otterkid

    Joined:
    Apr 23, 2023
    Posts:
    4
    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

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class FruitTree : MonoBehaviour
    6. {
    7.     GameObject parentSoil;
    8.  
    9.     public Soil soilScript;
    10.  
    11.     public bool apple;
    12.    
    13.     public Vector3 originalPos;
    14.  
    15.     public Transform[] appleSpawns;
    16.  
    17.     public bool grown;
    18.  
    19.     public GameObject apples;
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.         originalPos = transform.position;
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         if(apple == true && soilScript.timesWatered == 5 && grown == false)
    30.         {
    31.             GetComponent<Animator>().SetBool("Grown", true);
    32.  
    33.             transform.position = new Vector3(originalPos.x, originalPos.y + 1.25f, 0);
    34.  
    35.             foreach(Transform applespawn in appleSpawns)
    36.             {
    37.                 Instantiate(apples, applespawn.position, applespawn.rotation);
    38.             }
    39.  
    40.             grown = true;
    41.         }
    42.     }
    43.  
    44.     void OnTriggerEnter2D(Collider2D other)
    45.     {
    46.         if(parentSoil == null)
    47.         {
    48.             parentSoil = other.gameObject;
    49.  
    50.             Debug.Log("Parent Soil: " + parentSoil.name);
    51.  
    52.             soilScript = parentSoil.GetComponent<Soil>();
    53.         }
    54.        
    55.     }
    56. }
    57.  


    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Soil : MonoBehaviour
    6. {
    7.     public bool watered;
    8.  
    9.     public Color dark;
    10.  
    11.     Player playerScript;
    12.  
    13.     public GameObject outline;
    14.  
    15.     public Color unwatered;
    16.  
    17.     GameObject childPlant;
    18.  
    19.     public int timesWatered;
    20.     // Start is called before the first frame update
    21.     void Start()
    22.     {
    23.        
    24.     }
    25.  
    26.     // Update is called once per frame
    27.     void Update()
    28.     {
    29.         playerScript = GameObject.Find("Player").GetComponent<Player>();
    30.  
    31.         if(playerScript.soil == gameObject)
    32.         {
    33.             outline.SetActive(true);
    34.         }
    35.         else
    36.         {
    37.             outline.SetActive(false);
    38.         }
    39.  
    40.         if(childPlant == null)
    41.         {
    42.             timesWatered = 0;
    43.         }
    44.     }
    45.  
    46.     public IEnumerator Watering()
    47.     {
    48.         yield return new WaitForSeconds(3);
    49.  
    50.         watered = true;
    51.  
    52.         gameObject.GetComponent<SpriteRenderer>().color = dark;
    53.  
    54.         playerScript.doingAction = false;
    55.  
    56.         StartCoroutine(Drying());
    57.  
    58.         timesWatered++;
    59.     }
    60.  
    61.     public IEnumerator Planting(GameObject plant)
    62.     {
    63.         yield return new WaitForSeconds(.5f);
    64.  
    65.         Instantiate(plant, transform.position + new Vector3(0, .25f, 0), transform.rotation);
    66.  
    67.         playerScript.doingAction = false;
    68.  
    69.         childPlant = plant;
    70.     }
    71.  
    72.     IEnumerator Drying()
    73.     {
    74.         while(gameObject == gameObject)
    75.         {
    76.             yield return new WaitForSeconds(15);
    77.  
    78.             watered = false;
    79.  
    80.             gameObject.GetComponent<SpriteRenderer>().color = unwatered;
    81.         }
    82.     }
    83. }
    84.  
    also if you have any suggestion for my code, please feel free to suggest away :D
     
  2. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,709
    short answer -> prove it.

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

    otterkid

    Joined:
    Apr 23, 2023
    Posts:
    4
    wait so what should i do to fix it?
     
  4. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,709
    Well the nullreference wont lie, so there is your answer.. set it to the right thing
     
  5. otterkid

    otterkid

    Joined:
    Apr 23, 2023
    Posts:
    4
    but what's missing in my script? ive checked multiple times and everything seems to exist on line 29
     
  6. bugfinders

    bugfinders

    Joined:
    Jul 5, 2018
    Posts:
    1,709
    Thats why I said prove it, debug it, you will find out whats not there or right, and then you can fix it.
     
  7. otterkid

    otterkid

    Joined:
    Apr 23, 2023
    Posts:
    4
    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
     
  8. AngryProgrammer

    AngryProgrammer

    Joined:
    Jun 4, 2019
    Posts:
    490
    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.
     
    bugfinders likes this.
  9. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,672
    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