- Home /
Inventory String error
Basically I'm building an inventory system, that when your near an object it adds itself to a list within the player that is constantly changing of all pickup able items near it, when the object trigger is no longer colliding with the player it removes itself from that list, then when I'm in menu mode (press escape) if I click an object it sees if that objects tag is on the array, if it is not it wont do anything, else it will pick it up. basically, I assume most likely for the nearby items I will end up switching to a list, however for the inventory slots I want them to stay stationary, never changing as I need them to remain fixed that's why its a array, anyways here's the error:
Assets/Scripts/ItemControl.cs(57,48): error CS0019: Operator `==' cannot be applied to operands of type `UnityEngine.GameObject' and `string'
The Character script:
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;
private bool rendermenu;
// 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 Transform itemloc;
private Transform vest1loc;
private Transform VestHolder;
public bool collideobject;
public GameObject[] NearbyItems = new GameObject[10];
public GameObject[] InventorySlots = new GameObject[6];
private Rect windowRect = new Rect(10, 300, 104, 450);
public Texture2D DefaultItemIcon;
// 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))
{
for(int i = 0; i < NearbyItems.Length; i++)
{
if(NearbyItems[i].tag != null)
{
if(NearbyItems[i].tag == hit.collider.tag)
{
for(int d = 0; d < InventorySlots.Length; d++)
{
if(InventorySlots[d].tag == null)
{
Debug.Log(hit.collider.tag);
itemloc = GameObject.FindWithTag (hit.collider.tag).transform;
ItemControl ic = (ItemControl)itemloc.gameObject.GetComponent<ItemControl>();
VestHolder = GameObject.FindWithTag(ic.SlotTag).transform;
InventorySlots[d] = (hit.collider.gameObject);
ic.ispickedup = true;
InventorySlots[d].transform.parent = VestHolder.transform;
}
}
}
}
}
}
}
}
}
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);
// Calls Inventory GUI
windowRect = GUI.Window(0, windowRect, Domywindow, "Inventory" );
}
}
// 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");
}
// Inventory Gui
void Domywindow(int windowId)
{
int y = 20;
GUI.DragWindow(new Rect(0,0,300,20));
for(int i = 0; i < InventorySlots.Length; i++)
{
if(InventorySlots[i] != null)
{
ItemControl v1c = (ItemControl)InventorySlots[i].gameObject.GetComponent<ItemControl>();
if(InventorySlots[i].tag != null)
{
GUI.Button(new Rect(20,y,64,64),v1c.icon);
y += 70;
}
}
else
{
GUI.Button(new Rect(20,y,64,64),DefaultItemIcon);
y += 70;
}
}
}
}
And of course the item script:
public class ItemControl : MonoBehaviour {
public bool ispickedup;
public Texture2D icon;
public string SlotTag;
public GameObject Player;
// Use this for initialization
void Start () {
ispickedup = false;
}
void Held ()
{
SphereCollider sc = GetComponent<SphereCollider>();
if (ispickedup == true)
{
transform.localPosition = new Vector3(0, 0, 0);
transform.localRotation = Quaternion.identity;
rigidbody.Sleep();
sc.enabled = false;
}
else if (ispickedup == false)
{
rigidbody.WakeUp();
sc.enabled = true;
}
}
void OnTriggerEnter(Collider collide)
{
if (collide.gameObject.tag == "Player")
{
BaseGirlControl bgc = (BaseGirlControl)Player.GetComponent("BaseGirlControl");
for(int i = 0; i < bgc.NearbyItems.Length; i++)
{
if(bgc.NearbyItems[i] == null)
{
bgc.NearbyItems[i] = gameObject;
}
}
}
}
void OnTriggerExit(Collider collide)
{
if (collide.gameObject.tag == "Player")
{
if (ispickedup == false)
{
BaseGirlControl bgc = (BaseGirlControl)Player.GetComponent("BaseGirlControl");
for(int i = 0; i < bgc.NearbyItems.Length; i++)
{
if(bgc.NearbyItems[i] == tag)
{
bgc.NearbyItems[i] = null;
}
}
}
}
}
// Update is called once per frame
void Update () {
if (transform.parent != null && transform.parent.tag == SlotTag)
{
Held();
}
}
}
Never realized until now that's the biggest script I have ever wrote. lol thanks for your help guys, I'm off to work, be back in a few hours.
Is it this line?
if(bgc.NearbyItems[i] == tag)
What are you trying to do here?
Basically I was trying to see if the slot on the list equaled my tag if so remove it, so when I walk away from the item it removes itself from the array.
Answer by fafase · Oct 29, 2013 at 03:03 PM
if(bgc.NearbyItems[i] == tag)
The error tells you you are trying to compare an object with a string.
NearbyItem is an array of game object and tag is meant to be a string.
I guess you need:
if(bgc.NearbyItems[i].tag == tag)
This did the trick, but I'm probably going to have to switch to list anyways :P thanks though this did work.
Your answer
Follow this Question
Related Questions
Windows within windows 0 Answers
Is this considered bad coding 1 Answer
Possible to add array values within a to a script at runtime? 0 Answers