0

I am creating a game in unity and I am getting an error

Assets\player\player_Inventory.cs(23,16): error CS0119: 'pistol_interaction' is a type, which is not valid in the given context"

I am not understanding why am I getting the error, because before I got a another error which was null reference exception error. To solve it I read another article which I implemented in line 23

But I am still getting the error.

Here is the code:

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

public class player_Inventory : MonoBehaviour
{
    public GameObject Player;

    // Start is called before the first frame update
    void Start()
    {
    }

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKey("j"))
        {
            if (pistol_interaction != null)
            {
                pistol_interaction pistol = Player.AddComponent<pistol_interaction>();
                pistol.Pistol_interaction();
            }
        }
    }
}

// other script is

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

public class pistol_interection : MonoBehaviour
{
    [SerializeField]
    private GameObject model;

    private bool isEquipped = false;
    private bool inInventory = false;

    [SerializeField]
    private GameObject equipped;

    private Transform highlight;
    private RaycastHit raycastHit;
    private bool is_selected = false;

    // Start is called before the first frame update
    void Start()
    {
       // object_1 = GetComponent<Rigidbody>();
    }

    // Update is called once per frame
    void FixedUpdate()
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);

        if (Physics.Raycast(ray, out raycastHit))
        {
            highlight = raycastHit.transform;

            if (highlight.CompareTag("isPistol") && Input.GetKey(KeyCode.Mouse0))
            {
                is_selected = true;
            }
            else if (!(highlight.CompareTag("notSelectable")) && Input.GetKey(KeyCode.Mouse0) && isEquipped)
            {
                inInventory = true;
                is_selected = false;
            }
            else
            {
                is_selected = false;
            }
        }

        if (Input.GetKeyDown("e") && isEquipped)
        {
            inInventory = true;
            isEquipped = false;
        }

        if (is_selected)
        {
            equipped.SetActive(true);
            model.SetActive(false);
            isEquipped = true;
        }
        else if (!isEquipped)
        {
            equipped.SetActive(false);
        }

        if (inInventory)
        {
            equipped.SetActive(false);
            model.SetActive(false);
        }
    }

    public void Pistol_interaction()
    {
        Debug.Log("asfddf");
    }
}
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Student.
  • 1
  • 1
  • Does this answer your question? [How to solve '...is a 'type', which is not valid in the given context'? (C#)](https://stackoverflow.com/questions/2278931/how-to-solve-is-a-type-which-is-not-valid-in-the-given-context-c) – Jay May 04 '23 at 18:22

1 Answers1

0

To check whether a GameObject has a component attached, you can use the TryGetComponent or GetComponent method like this:

if(!Player.TryGetComponent(out pistol_interection pistol)
{
    pistol = Player.AddComponent<pistol_interection>();
    pistol.Pistol_interection();
}

A better approach would be to cache a reference to the component:

public GameObject Player;
private pistol_interection pistol;

void Update()
{
    if(Input.GetKey("j"))
    {
        if(!pistol)
        {
            pistol = Player.AddComponent<pistol_interection>();
            pistol.Pistol_interection();
        }
    }
}
shingo
  • 18,436
  • 5
  • 23
  • 42
  • sorry I am a biginer and I am not understanding the error itself. – Student. May 04 '23 at 05:21
  • can you give resion why I am gating this error – Student. May 04 '23 at 05:22
  • `pistol_interection` is a type, you cannot check if it is null, `pistol_interection == null` is syntactically incorrect. – shingo May 04 '23 at 05:23
  • I applied your correction but now I am gating a null reference error – Student. May 04 '23 at 05:29
  • That's another question, you can read [this](https://docs.unity3d.com/Manual/NullReferenceException.html) and [this](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it?r=SearchResults&s=1%7C388.1147), if you still cannot solve it, please ask a new question. – shingo May 04 '23 at 05:33
  • Should `if(!Player.TryGetComponent(out pistol_interection pistol)` not be `if(Player.TryGetComponent(out pistol_interection pistol)` (no negation). It looks like you're going to be using the valule of `pistol` only if it is null otherwise – Jay May 04 '23 at 18:20
  • @Jay If there is already a component attached it's not necessary to call `AddComponent`, Maybe the next line should be moved outside of the if block, but I just followed the original question. – shingo May 05 '23 at 04:16
  • @shingo correct, my bad – Jay May 05 '23 at 15:31