- Home /
i... kinda made it work? i don't know what's going on anymore
Array element coming as not null
i'm trying to make an inventory system using an array of gameobjects, and i'm using a bool to keep track if the inventory is full or not, but the "if [2] !=" always comes as true, even though the space is null. this is my code, idk what to do
void Update()
{
if (sylladex [2] != null) {
isFull = true;
} else {
isFull = false;
}
}
Can we see the full script? It's difficult to deter$$anonymous$$e the issue with just this much.
that last bit there, the bool, is not really necessary, i was just desperate
still am Edit: I can't see the script i posted on this reply anymore, is it there still? whats going on
public class Sylladex : $$anonymous$$onoBehaviour {
public string[] sylladex = new string[3];
public bool isFull = false;
public void AddItem(string item)
{
if (sylladex [0] == null) {
sylladex [0] = item;
} else if (sylladex [1] == null) {
sylladex [1] = sylladex [0];
sylladex [0] = item;
} else if (sylladex [2] == null) {
sylladex [2] = sylladex [1];
sylladex [1] = sylladex [0];
sylladex [0] = item;
} else {
//drop [2]
sylladex [2] = sylladex [1];
sylladex [1] = sylladex [0];
sylladex [0] = item;
}
}
public void DropItem()
{
Instantiate (Resources.Load(sylladex [0]), transform.position, Quaternion.identity);
}
void Update()
{
if (sylladex [0] == null && sylladex [1] != null) {
sylladex [0] = sylladex [1];
sylladex [1] = null;
if (sylladex [2] != null) {
sylladex [1] = sylladex [2];
sylladex [2] = null;
}
}
if (sylladex [2] != null) {
isFull = true;
} else {
isFull = false;
}
{
Debug.Log (sylladex [2]);
}
}
}
here's the script, sorry for the delay, only noticed this comment now
It's no problem. From the question being closed, it looks like you got it to work?
I'll detail my thoughts anyway:
When you create a public array in Unity, an instance of it gets created by the Unity Serialization System (which is why you don't have to initialize the array).
When an array is initialized by the Unity Serializer, it gets automatically populated with default values for that type. (The default value of a string is an empty string, which is not the same as null)
Since you are managing the array only using Add Item and Delete Item, you can choose to make the array private. This way, the Unity Serializer will not modify your array (you will also not be able to see it in the inspector for your script). This way, you won't have a null issue.
To take it further, I recommend you use a List structure for the sylladex, ins$$anonymous$$d of an array. A list makes it fairly easy to add or remove items and move others.
Here's a full example:
using UnityEngine;
using System.Collections.Generic;
public class Sylladex : $$anonymous$$onoBehaviour
{
public int itemLimit = 2;
private List<string> sylladex = new List<string>();
private bool isFull = false;
public void AddItem(string item)
{
if (isFull)
{
if (sylladex.Count > 0)
{
//Drop the oldest item
string latestItem = sylladex[0];
Instantiate(Resources.Load(latestItem), transform.position, Quaternion.identity);
//Remove the oldest item from the list
sylladex.RemoveAt(0);
}
}
//Add a new item to the end of the list
sylladex.Add(item);
//If the number of items in the sylladex equals the item limit, set isFull = true
isFull = sylladex.Count == itemLimit;
}
public void DropItem()
{
//If there are no items, do nothing
if (sylladex.Count == 0)
return;
//Drop the item that was added last
int indexOfLatestItem = sylladex.Count - 1;
string latestItem = sylladex[indexOfLatestItem];
Instantiate(Resources.Load(latestItem), transform.position, Quaternion.identity);
//Remove the latest item from the list
sylladex.RemoveAt(indexOfLatestItem);
//If the number of items in the sylladex equals the item limit, set isFull = true
isFull = false;
}
}
There are more optimal ways of doing this, but this should be plenty to get you started. I also recommend reading up on Queues and Stacks for your data structures.
Hope that helps!
public class Sylladex : $$anonymous$$onoBehaviour {
public string[] sylladex = new string[3];
public bool isFull = false;
public void AddItem(string item)
{
if (sylladex [0] == null) {
sylladex [0] = item;
} else if (sylladex [1] == null) {
sylladex [1] = sylladex [0];
sylladex [0] = item;
} else if (sylladex [2] == null) {
sylladex [2] = sylladex [1];
sylladex [1] = sylladex [0];
sylladex [0] = item;
} else {
//drop [2]
sylladex [2] = sylladex [1];
sylladex [1] = sylladex [0];
sylladex [0] = item;
}
}
public void DropItem()
{
Instantiate (Resources.Load(sylladex [0]), transform.position, Quaternion.identity);
}
void Update()
{
if (sylladex [0] == null && sylladex [1] != null) {
sylladex [0] = sylladex [1];
sylladex [1] = null;
if (sylladex [2] != null) {
sylladex [1] = sylladex [2];
sylladex [2] = null;
}
}
if (sylladex [2] != null) {
isFull = true;
} else {
isFull = false;
}
{
Debug.Log (sylladex [2]);
}
}
}
posting it again just because
Is this an array of GameObjects, or an array of Strings?
Do you ever assign null to sylladex[2] anywhere in your code?
It's an array of gameobjects, i only assign null to sylladex [2] on some other instance that is actually working,
void Update()
{
if (sylladex [0] == null && sylladex [1] != null) {
sylladex [0] = sylladex [1];
sylladex [1] = null;
if (sylladex [2] != null) {
sylladex [1] = sylladex [2];
sylladex [2] = null;
}
}
if (sylladex [2] != null) {
isFull = true;
} else {
isFull = false;
}
Debug.Log (sylladex [2]);
}
after this function happens, it correctly displays that [2] is null... but that only happens after an gameobject enters and leaves that index
EDIT: wait sorry i forgot, it's an array of strings, it used to be gameobjects, changed it today
is the "sylladex" array declared public? If so, Unity will initialize it with empty strings (which are not null), since the Unity Serialize doesn't support nulls.
Answer by Martin_Gonzalez · Mar 26, 2018 at 07:02 PM
Try doing Debug.Log(sylladex[2])
to see what is containing.
the log comes out as [ ] Unity.Engine.Debug.Log:(Object) without the brackets, it just comes out as an empty text
So there is an object but its empty, if not it will log null.
Can you post more of your code?
So, it seems to be correctly noticing that the index is empty now, but only because i erased all of the lines of my inventory code. I readded it to print here
public void AddItem(string item)
{
if (sylladex [0] == null) {
sylladex [0] = item;
} else if (sylladex [1] == null) {
sylladex [1] = sylladex [0];
sylladex [0] = item;
} else if (sylladex [2] == null) {
sylladex [2] = sylladex [1];
sylladex [1] = sylladex [0];
sylladex [0] = item;
} else {
//drop [2]
sylladex [2] = sylladex [1];
sylladex [1] = sylladex [0];
sylladex [0] = item;
}
}
the problem with this section of the code was on the last one, it was suposed to be activated only when [2] was full, and do an instantiate to spawn the [2] gameobject prefab, but ins$$anonymous$$d it was happening everytime i tried to pick an item up
i readded those lines and did the Debug.Log again, still comes out as empty, but it triggers the bool to true, still. (bool was suposed to trigger only when [2] != not null)
Answer by cumulowinter · Mar 26, 2018 at 10:35 PM
Ah also it seems that if i pick stuff up until there is something on [2], and drop it, it now recognizes that [2] != null
What you are doing seems to be a Queue. You really need the index 2? Or you are saying that the number 2 is the last item of the array?
No problem, we all started once.
Tell me who calls AddItem and when
Try ins$$anonymous$$d of passing current.GetComponent().name, send any string like “item” just the check that that part of the code is not the problem
Try this
void Update() {
if (Input.Get$$anonymous$$eyDown ($$anonymous$$eyCode.E) && currentInterObj)
{
if (currentInterObjScript.isPickable) {
sylladex.AddItem (“item”);
currentInterObj.Send$$anonymous$$essage ("PickUp");
currentInterObj = null;
}
}
Follow this Question
Related Questions
how to check for GameObject is null in array with random 1 Answer
How to access gameobject array set in inspector? 2 Answers
Simple Looping drag and drop game 1 Answer
I have an error, script should check for null or not destroy game objects 2 Answers
How to check if a specific gameobject is in the array? 1 Answer