- Home /
Inventory does not contain a definition for inven ... but it does
I have 2 scripts. On called Inventory and one called PickUp. I am getting the error: error CS1061: Type Inventory' does not contain a definition for Inven' and no extension method
Inven' of type Inventory' could be found. Are you missing an assembly reference? Here is the inventory script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Inventory : MonoBehaviour {
public List<Obj> Inven;
public Image slot1;
public Image slot2;
public Image slot3;
public Image slot4;
public Image slot5;
public Obj selectedSlot;
public int selected;
// Use this for initialization
void Start () {
selected = 0;
}
// Update is called once per frame
void Update () {
selectedSlot = Inven[selected];
slot3.sprite = selectedSlot.img;
}
}
and here is the PickUp script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class PickUp : MonoBehaviour {
public Inventory inv;
public Text txt;
void OnTriggerEnter(Collider other)
{
if (Input.GetKey(KeyCode.E))
{
if (other.gameObject.CompareTag("Pick Up"))
{
inv.Inven.Add(other.GetComponent<Obj>());
other.gameObject.SetActive(false);
}
}
}
}
and here is the Obj script :
public class Obj : MonoBehaviour {
public bool IsGrounded;
public bool IsStable;
public float hp;
public string Name;
public string desc;
public Sprite img;
// Update is called once per frame
void Update () {
if (IsStable == false)
{
gameObject.AddComponent<Rigidbody>();
}
if (IsGrounded)
{
IsStable = true;
}
}
}
Are you sure you don't have 2 scripts named Inventory
in your project? If you use the Go to definition feature of your IDE ($$anonymous$$onodevelop or Visual Studio), are you sure you are redirected to the correct file?
Sometimes closing the IDE and reopening and Unity fixes itself. Also, I think it is about to crash anyway as I cannot see where you would instantiate the list
Inven = new List<Obj>();
Because the List is declared as public, Unity serializes it and instantiates it automativally.
Answer by meat5000 · Jan 30, 2018 at 10:36 AM
You seem to have declared your List but not Initialised it.
'List myList = new List'
The new List bit is important.
So for you:
public List<Obj> Inven = new List<Obj>();
Your answer
![](https://koobas.hobune.stream/wayback/20220612151605im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Console error is given without filename when compiling 2 Answers
IUnityEventQueue - how do I use it? 2 Answers
Small error C# 2 Answers