Raycast correctly storing gameobject but still getting null reference error

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

public class RaycastVillagerPickUp : MonoBehaviour

{

    GameObject villagerObject;
    private Vector3 godHandEmptyPosition;
    VillagerScript villagerScript;
    public bool isVillagerBeingCarried = false;
    public bool doesVillagerExist = false;


    private void Update()

    {

        FindVillager();

        if (Input.GetMouseButtonDown(1))

        {
            isVillagerBeingCarried = true;
            PickUpVillager();
            Debug.Log("Villager Picked Up");

        }

        if (isVillagerBeingCarried == true)

        {
            godHandEmptyPosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
            villagerObject.transform.position = godHandEmptyPosition;
        }
 

        else if (isVillagerBeingCarried == false)

        {
            return;
        }

    }


    public void PickUpVillager()

    {
        RaycastHit hit;

        if (Physics.Raycast(transform.position, Vector3.down * 1000, out hit) && hit.collider.tag == "Villager" )

        {
                VillagerScript villagerScript = hit.transform.gameObject.GetComponent<VillagerScript>();
                villagerObject.GetComponent<VillagerScript>().VillagerPickedUp = true;
        }
    }

    public void FindVillager()

    {
        RaycastHit hitFindVillager;

        if (Physics.Raycast(transform.position, Vector3.down * 1000, out hitFindVillager) && hitFindVillager.collider.tag == "Villager")

        {
            GameObject villagerObject = hitFindVillager.transform.gameObject;
            doesVillagerExist = true;
            Debug.Log(villagerObject);
        }

    }





}

I’ve probably just been staring at this code for too long and not noticing the obvious but I’ve been double checking my code and using Debug.Log, my raycast is detecting the villagers correctly and storing them but whenever I right click on the villager which should then change it’s transform position to the hand object I get null reference errors constantly and the villager doesn’t even follow my hand object correctly.

The error is specifically pointing towards this line of code.

villagerObject.transform.position = godHandEmptyPosition;

script is not setting “villagerObject” anywhere, can try inside physics.raycast,

villagerObject = hit.gameObject;

you could also use raycast layermask instead of comparing tag:

Could you be a bit more detailed with your explanation? hit.gameObject gives errors.

Edit: OH! I just understood it, so it looks like what’s been causing all of my issues is that because I was declaring a GameObject villagerObject that was making an entirely separate villagerObject within the RayCast, no wonder I was running into so many problems.

minor fix to mgear’s pesudo code:

villagerObject = hit.transform.gameObject;

That’s really interesting, I thought I would need to declare a type for that to work, so in order for the other code to work I just need to declare the villagerObject again and then all the errors should go away.

Ah, so now I have a better understanding of this the problem is technically ‘solved’ but I need to make it so that the villagerObject’s transform position attached to the transform.position of my GodHandEmpty inside update I thought that the correct way to do it and the raycast would store the villagerObject for me and I could reference that to have the VillagerObject attach itself to the GodHandEmpty but even though I could get it working those errors would keep popping up?

What’s a better way to do this? I was thinking of maybe even storing what the Raycast hits in an array or a list.

Just to let people know it looks like I was right and using a list to grab the villager was a much better option as it stored the gameobject permanently meaning I could access it far easier along with any components or other data I needed, it’s also drastically reduced the amount of code that I’m using.

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

public class RaycastVillagerPickUp : MonoBehaviour

{
 
    private Vector3 godHandEmptyPosition;
    VillagerScript villagerScript;

    public List<GameObject> villagerObjects;

    private void Update()

    {

        if (Input.GetMouseButtonDown(1))

        {
            PickUpVillager();
            Debug.Log("Villager Picked Up");

        }

        if (villagerObjects.Count > 0)

        {
            godHandEmptyPosition = new Vector3(gameObject.transform.position.x, gameObject.transform.position.y, gameObject.transform.position.z);
            villagerObjects[0].transform.position = godHandEmptyPosition;
        }



    }


    public void PickUpVillager()

    {
        RaycastHit hit;

        if (Physics.Raycast(transform.position, Vector3.down * 1000, out hit) && hit.collider.tag == "Villager")

        {
            villagerObjects.Add(hit.transform.gameObject);
            villagerObjects[0].GetComponent<VillagerScript>().VillagerPickedUp = true;
        }
    }





}

Now I just need to clear the list when I drop the villager or prevent anything happening when another villager is clicked and have it return to the terrain transform and I’m done and it should mean I don’t get errors now like I did with the raycast method.