So i am making my own inventory system, just as a means to learn C# and Unity.
I have come across this strange problem. I have a list consisting of a custom class.
The custom class looks like so:
public class InventoryItem
{
public string ItemName;
public bool isStackable;
public int itemAmount;
public Texture2D itemImage;
public GameObject item;
public void generateInventoryItem(string name, bool stackable, int amount, Texture2D image, GameObject theItem)
{
ItemName = name;
isStackable = stackable;
itemAmount = amount;
itemImage = image;
item = theItem;
}
}
For some reason when i add an item to my class list, it works with everything except for the gameObject.
The code used:
The “itemName”, “isStackable” and “itemImage” works just fine, but the “item” does not work. It is just empty/null
I have made a debog.log(item) just above, to see if the item is sent to the function properly, and it is! So it just wont add it to the class for some reason. I probably just missed something. Hoping someone can help me.
I dont see anything wrong with the code, even if i dont really get why you would use this generator function instead of just using the constructor - because that’s already what you are using it for in a more complicated way. That should not be related to the problem tho.
Where or how do you test that the item you put in there is null? And where did you put your check to see if the gameobject is indeed not null?
I am probably not doing things the most efficient way. I am VERY new to C# or programming in general, so i am just trying to do things without tutorials or copy/paste code
I have simply just made the class serializable and the list public. That way i can keep an eye on it in the inspector. In the inspector window i can see that the item gameobject returns null. I also checked through a debug.log
Let me know if there is anything else you need to see. But as mentioned, i know for a fact that the function does get the gameobject correctly, it is just not getting from the function into the class for some reason…
Edit 1: And thank you for your response by the way. Any means to help is really nice of you!
I mentioned it in my other answer, but here you have it
I have simply just made the class serializable and the list public. That way i can keep an eye on it in the inspector. In the inspector window i can see that the item gameobject returns null. I also checked through a debug.log on both the item and the list object.
I meant what are you doing when it’s null? I’m assuming it’s in play mode at runtime, but how are you getting the reference to the other object? What is the other object? Add Debug.Log("Item", item); at the top of your AddToInventory function, pause the editor and click on the message, it should highlight the actual GameObject in the inspector. Does it still exist? Nothing you’ve posted code wise should be giving you the issues you’re having.
Sorry about that, i misunderstood your question in that case.
The object is a prefab, which it gets from an object that is being deleted. Is that the problem?
The process goes like this:
Item is on ground
Item on ground has a script on it that holds several values, and also a reference to a prefab of a gameobject.
AddToInventory function is run, where it should grab the gameObject prefab from the item on ground (It does not grab the item on ground directly)
Item on ground is destroyed
Since i stored it as a prefab in my projects folder, i dont see how destroying the object should interfere with this. Also, when i can a debug in the AddToInventory function, it did get the item. It is ONLY adding the item to the inventoryList that is a problem for some reason. I hope this makes sense.
Edit 1: I realize now that it is the fact that i am destroying the gameObject. Even though that i am taking the prefab of the object and not the object itself, it still looses its reference after being destroyed… The question is now, how do i stop this behaviour? I want to get the prefab of the gameObject and not the actual prefab. But i want to get it through the item itself, and not somewhere else, so i can keep everything in one script for easier setup in the future.
Edit 2: Alright, i found the solution. I for some reason had to set the preset again, when instantiating the object on the ground. Else it would reference itself. I find this behaviour to be odd, but a fairly simple workaround when you know the problem.