0
\$\begingroup\$

I have my item class (items.cs) not attached to any gameObjects, I want to use items for all items in my game so I can use arrays as inventories.

I have Stone.cs which is attached to a gameObject. On the line inv = items.item(100f, "Stone", "Rock used for building"); I get a compiler error Non-invocable member 'items.item' cannot be used like a method. I am confused by this as I copied it from the Unity class tutorial, just splitting creating the item to another script.

items.cs

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

public class items : MonoBehaviour{
    public class item{
        public float iQuan;
        public string iName;
        public string iDesc;

        public item(float iQ, string iN, string iD){
                iQuan = iQ;
                iName = iN;
                iDesc = iD;
        }

    }
}

Stone.cs

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Stone : MonoBehaviour {
    // Use this for initialization
    public items.item inv; 
    public float iMax;
    public float iCur;
    void Start () {
        inv = items.item(100f, "Stone", "Rock used for building"); 
    }

    // Update is called once per frame
    void Update () {

    }
}
\$\endgroup\$
1
  • \$\begingroup\$ You should probably learn programming before you start using Unity. \$\endgroup\$
    – Bálint
    Commented Dec 10, 2017 at 16:04

1 Answer 1

2
\$\begingroup\$

item is not a method. It's an object. If you want to create an instance of item, then you need to use the new keyword:

items.item inv = new items.item(...)

Also, the first letter of class names should be uppercase, something like

Items.Item inv = new Items.Item(...)
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .