GetComponent not working properly

Hi guys, I’m sort of new to scripting but I’ve followed several guides and instructions to the letter and cannot figure out why this isn’t working -

The basic idea of the script is that when the player clicks on something it will check what the object is - if it is a chest then I want to check that objects variables to see a few things about that chest, starting with if it has been looted already, however get component is not working.

here are all the relevant lines of code, please help!

public class Interact : MonoBehaviour {
    private Vector3 playerLoc;
    private Vector3 interactLoc;
    public GameObject player;
    private GameObject interactedObject;

        void Update ()
    {
        if (Input.GetMouseButtonDown (0))
        {     
            GetInteraction ();
        }
    }

    void GetInteraction()
    {
        Ray interactionRay = Camera.main.ScreenPointToRay (Input.mousePosition);
        RaycastHit interactionInfo;
        if (Physics.Raycast (interactionRay, out interactionInfo, Mathf.Infinity)) {
        interactedObject = interactionInfo.collider.gameObject;
        playerLoc = player.transform.position;
        interactLoc = interactedObject.transform.position;
            Debug.Log ("you touch" + interactedObject);


            // Check if object is a chest
            if (interactedObject.tag == "Chest")
            {
                float dist = Vector3.Distance (playerLoc, interactLoc);
                Debug.Log (dist);
                if (dist >= 1.8f)
                {
                    Debug.Log ("You need to get closer to open that.");
                }
                else
                {
                    ChestAction ();
                }


    //Interact with Chest script
    public virtual void ChestAction(){
        Debug.Log ("You are close enough to the chest.");

[COLOR=#ff0000]        bool lootable = interactedObject.GetComponent<InteractChest>.chestLooted;[/COLOR]

            if (lootable == (false)) {
        } 
}


And here is the code from the 'InteractChest' script that is attached to the chest object that should be getting interacted with.

public class InteractChest : MonoBehaviour {

    public int chestGold;
    public bool chestLocked = (false);
    public bool chestLooted = (false);
    public string chestItems = ("None.");

    void Awake(){
        chestGold = Random.Range(5, 32);
    }
}

No matter where I search, I can’t find a way on how to get the information from chestLooted that is attached to whatever chest is being interacted with, what am I doing wrong? :frowning:

Edit: Thanks guys, I just realized the error! and changed the snippet, srry about that

2 things.

Use code tags when posting code snippets.

What do you mean by not working?

Is it because GetComponent is a method and you should be concluding the method call proprely?

bool lootable = interactedObject.GetComponent<InteractChest>().chestLooted;

Good spot. Posters need to give more details than just saying something vague like “it’s not working”. This is a compile error - posting those details would help.

1 Like

Agreed.
I was tempted to ask if it’s just a NULL pointer exception. But I’ve encountered this specific mistake so many times that I can pretty much call it out when I’m half asleep. :slight_smile:

Edit:
Taking another look at the script now, I don’t think NULL pointer would even be possible.