- Home /
Inventory array add error
Well I've used this exact command before and I never recieved an error so I assume its somewhere else in the script.
The Error:
Assets/Scripts/BaseGirlControl.cs(121,64): error CS1061: Type `UnityEngine.GameObject[]' does not contain a definition for `Add' and no extension method `Add' of type `UnityEngine.GameObject[]' could be found (are you missing a using directive or an assembly reference?)
The Code.
using UnityEngine;
using System.Collections;
[AddComponentMenu("Camera-Control/Mouse Look")]
public class BaseGirlControl : MonoBehaviour {
// Health & Heat
public int maxhealth = 100;
public int curhealth = 100;
private float healthbarlength;
public int maxheat = 100;
public int curheat = 100;
private float heatbarlength;
public Transform GameController;
// movement
public float animSpeed = 1.5f;
public float speed = 10.0f;
public float RotationSpeed = 100;
private Animator anim;
// in hand items
public string pickupTag;
private Transform pickuploc;
private Transform rightPalmloc;
private bool RightHandEquip;
public bool collideweapon;
// Non in-hand items
private RaycastHit hit;
private Ray ray;
private bool rendermenu;
public GameObject[] InventorySlots = new GameObject[3];
// Use this for initialization
void Start () {
pickupTag = null;
anim = GetComponent<Animator>();
RightHandEquip = false;
collideweapon = false;
}
// Update is called once per frame
void Update () {
HandlesGUI hg = (HandlesGUI)GameController.GetComponent("HandlesGUI");
if (hg.Rendermenu == true)
{
rendermenu = true;
}
else
{
rendermenu = false;
}
AddjustCurrentHealth(0);
AddjustCurrentHeat(0);
float h = Input.GetAxis("Horizontal");
float v = Input.GetAxis("Vertical");
anim.SetFloat("Speedz", v);
anim.SetFloat("Speedx", h);
anim.speed = animSpeed;
if (Input.GetMouseButton(1))
{
}
else
{
if(rendermenu == false)
{
transform.Rotate(0, Input.GetAxis("Mouse X"), 0 * Time.deltaTime * speed);
}
}
// Handles weapons / In hand items
if (Input.GetButton("InteractionKey"))
{
if (collideweapon == true)
{
if (pickupTag != null)
{
pickuploc = GameObject.FindWithTag(pickupTag).transform;
pickuploc.GetComponent<WeaponControl>().ispickedup = true;
rightPalmloc = GameObject.FindWithTag("GrippingHand").transform;
pickuploc.transform.parent = rightPalmloc.transform;
RightHandEquip = true;
}
}
}
if (Input.GetButton("DropWeaponKey"))
{
if (RightHandEquip == true)
{
pickuploc = GameObject.FindWithTag(pickupTag).transform;
pickuploc.GetComponent<WeaponControl>().ispickedup = false;
RightHandEquip = false;
}
}
// Handles Clicking in gui mode to pick up items
if(rendermenu == true)
{
ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if(Input.GetButtonDown("Fire1"))
{
if(Physics.Raycast(ray,out hit,20))
{
Debug.Log(hit.collider.tag);
if(hit.collider.tag == "Item")
{
InventorySlots.Add(hit.collider.gameObject);
}
}
}
}
}
void OnGUI ()
{
// Health system
if (rendermenu == true)
{
GUI.Box(new Rect(10, 10, 120, 20), "Health");
GUI.Box(new Rect(10, 30, healthbarlength, 20), curhealth + "/" + maxhealth);
GUI.Box(new Rect(10, 50, 120, 20), "Heat");
GUI.Box(new Rect(10, 70, heatbarlength, 20), curheat + "/" + maxheat);
}
}
// Health system
public void AddjustCurrentHealth(int adj) {
curhealth += adj;
if(curhealth < 0)
curhealth = 0;
if(curhealth > maxhealth)
curhealth = maxhealth;
if(maxhealth < 1)
maxhealth = 1;
healthbarlength = (Screen.width / 2) * (curhealth / (float)maxhealth);
if(curhealth == 0)
Application.LoadLevel("MainMenu");
}
public void AddjustCurrentHeat(int adjh) {
curheat += adjh;
if(curheat < 0)
curheat = 0;
if(curheat > maxheat)
curheat = maxheat;
if(maxheat < 1)
maxheat = 1;
heatbarlength = (Screen.width / 2) * (curheat / (float)maxheat);
if(curheat == 0)
Debug.Log("Heatcold");
}
}
Second stupid question today, sorry guys this will be my last. Thanks allot, I apologize for the trouble.
Answer by robertbu · Oct 17, 2013 at 12:17 AM
InventorySlots ia array which doesn't have an 'Add' method. The 'Add' method is in .NET Lists. So make this work you would declare InventorySlots like this on line 40:
public List<GameObject> InventorySlots = new List<GameObject>();
And at the top of the file:
using System.Collections.Generic;
The only problem with that, and the reason why I used arrays in the first place was I didn't want it to add it to the end of the list, thus making it longer, I wanted to use up one of the 3 slots, then when dropping it empty out said slot. Is there no way to use arrays? or make a list behave in this manner?
I'm not sure how you solved your problem. With an array, you can just assign a slot.
InventorySlots[2] = something;
Basically I just made a list that when I try to add an item if it = 3 it wont let you add anymore, simple enough, thanks anyways
Your answer

Follow this Question
Related Questions
C# Adding to an Array 1 Answer
Need help with array script 1 Answer
Lists, Is a type but is used like a variable Problem C# 1 Answer