- Home /
Weird issues when adding an object to a variable.
Hey there! Today, i have started to work on a project, everything is working perfectly, but the inventory system.
I have gameobjects with an script attached, and another gameobject which is the player. The player has a script named "invManager" which contains all the code required for managing the inventory during the play. On the object i'm willing to pick up, i have another object attached, with an script which calls the invManager on the player to get himself added to the inventory list, then, the inventory does a quick check to see what slot is free, and places the pickable object there, but there seems to be a weird issue:
Whatever i do, the object gets attached to the slot 9 (the last one), but the real free slot is 1...
Here is the code on the pickable object (It does not destroys himself yet):
using UnityEngine;
using System.Collections;
public class InputBlock : MonoBehaviour {
public Color highColor;
private Color startcolor;
private bool showGUI = false;
public GameObject mouseObject;
public mouseManager mouseScript;
public invManager invScript;
void Start()
{
mouseScript = mouseObject.GetComponent<mouseManager>();
invScript = mouseObject.GetComponent<invManager>();
}
void OnMouseEnter()
{
startcolor = renderer.material.color;
renderer.material.color = highColor;
}
void OnMouseExit()
{
renderer.material.color = startcolor;
}
void OnMouseDown() //TODO: Make a GUI (Not needed yet)
{
invScript.checkFreeSlots ();
invScript.addBlock(this.gameObject);
}
}
And the invManager code:
using UnityEngine;
using System.Collections;
public class invManager : MonoBehaviour { //Script to manage inventory, and placement of blocks.
//Slot definitions (Starts by 1)
public GameObject slot1 = null;
public GameObject slot2 = null;
public GameObject slot3 = null;
public GameObject slot4 = null;
public GameObject slot5 = null;
public GameObject slot6 = null;
public GameObject slot7 = null;
public GameObject slot8 = null;
public GameObject slot9 = null;
//End of slot definition
public int freeSlot = 1; //Used to easily tell other scripts which slot to place stuff at
void Start () {
slot1 = null;
slot2 = null;
slot3 = null;
slot4 = null;
slot5 = null;
slot6 = null;
slot7 = null;
slot8 = null;
slot9 = null;
print ("Inventory system initialized");
}
void Update () {
checkFreeSlots();
print (freeSlot);
}
public void checkFreeSlots(){ //Barebones function to check slots
//The smaller slot is the chosen.
if (slot1 == null) {
freeSlot = 1;
return;
}
if (slot2 == null) {
freeSlot = 2;
return;
}
if (slot3 == null) {
freeSlot = 3;
return;
}
if (slot4 == null) {
freeSlot = 4;
return;
}
if (slot5 == null) {
freeSlot = 5;
return;
}
if (slot6 == null) {
freeSlot = 6;
return;
}
if (slot7 == null) {
freeSlot = 7;
return;
}
if (slot8 == null) {
freeSlot = 8;
return;
}
if (slot9 == null) {
freeSlot = 9;
print ("Chosen 9");
return;
}
}
public void addBlock(GameObject blockObject) //For some reason, always adds to slot 9
{
checkFreeSlots ();
print (freeSlot); //Debug (Always prints 9 for some reason...
print ("[INV] Adding block...");
if (freeSlot == 1) {
slot1 = blockObject;
print ("[INV] Success! Added Block to slot:"+freeSlot);
return;
}
if (freeSlot == 2) {
slot2 = blockObject;
print ("[INV] Success! Added Block to slot:"+freeSlot);
return;
}
if (freeSlot == 3) {
slot3 = blockObject;
print ("[INV] Success! Added Block to slot:"+freeSlot);
return;
}
if (freeSlot == 4) {
slot4 = blockObject;
print ("[INV] Success! Added Block to slot:"+freeSlot);
return;
}
if (freeSlot == 5) {
slot5 = blockObject;
print ("[INV] Success! Added Block to slot:"+freeSlot);
return;
}
if (freeSlot == 6) {
slot6 = blockObject;
print ("[INV] Success! Added Block to slot:"+freeSlot);
return;
}
if (freeSlot == 7) {
slot7 = blockObject;
print ("[INV] Success! Added Block to slot:"+freeSlot);
return;
}
if (freeSlot == 8) {
slot8 = blockObject;
print ("[INV] Success! Added Block to slot:"+freeSlot);
return;
}
if (freeSlot == 9) {
slot9 = blockObject;
print ("[INV] Success! Added Block to slot:"+freeSlot);
return;
}
}
}
The results of clicking on the pickable object: "Success! Added block to slot 9" And i'm specting: "Success! Added block to slot 1"
No idea why this happens... Thanks for your help!
Cheers!
Answer by nu-assets · Jan 28, 2015 at 08:31 AM
I would suggest an iterative approach (makes your script shorter and more clear):
using UnityEngine;
using System.Collections;
//Script to manage inventory, and placement of blocks.
public class InvManager : MonoBehaviour
{
public bool Full
{
get
{
return GetNextFreeSlot() < 0;
}
}
//Slot definitions (Starts by 1)
public GameObject[] Slots = new GameObject[9];
void Start()
{
print("Inventory system initialized");
}
public int GetNextFreeSlot()
{
//The smaller slot is the chosen.
for (int i = 0; i < Slots.Length; i++)
{
GameObject go = Slots[i];
if (go == null)
{
return i;
}
}
// Return -1 if no empty slot found
return -1;
}
// Adds a block to the inventory is not full
public void AddBlock(GameObject blockObject)
{
if (!Full)
{
int slotIndex = GetNextFreeSlot();
print("[INV] Adding block @ " + slotIndex);
Slots[slotIndex] = blockObject;
}
else
{
print("[INV] Inventory is full...");
}
}
}
And call it like this:
void OnMouseDown()
{
invScript.AddBlock(this.gameObject);
}
But keep in mind that the instance 'invScript' should be globally available during runtime (singleton, static, ...).
I have also refactored your script to fit the common c# conventions.. ;)
Well put together (you have my upvote), but the only shitty part is that we still don't know why the value assigned was 9.
I have removed all unnecessary calls to 'checkFreeSlots' (GetNextFreeSlot) and suggested a durable hold of the data. This makes it easier to find the buggy part. If that would not work, there must be another thing causing this misbehaviour.
Your answer
Follow this Question
Related Questions
Index out of range exception 0 Answers
Inventory Cursor Location 1 Answer
Using PlayerPrefs to Imitate a players inventory 1 Answer
GUI.Button is acting funky. 2 Answers
How to manage inventory 'objects' in C# for behind the scenes inventory stuff 4 Answers