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