- Home /
Classes : extend MonoBehaviour issues
So i have the class "Item" for all the items in the game that the player will be able to interact with. I then have an array EquipMenu with items the player currently has equipped.
public class Item
{
var Item_ID : int;
var Name : String;
var Description : String;
var icon : Texture2D;
var Health : float;
var Energy : float;
var Damage : float;
var rarity : Rarity;
var itemType : ItemType;
}
At the moment when i need to Instantiate an "Item" i get the Item ID as follows:
var item_id : int = EquipMenu[equip_selection].Item_ID;
I then use the Item ID to select the prefab from an array of GameObjects to then instantiate.
I would much rather define the prefab in the class as follows:
public class Item
{
var prefab : GameObject;
}
But this requires the class to extend MonoBehaviour, i believe, in order to hold the prefab.
This then means that i cannot define the items in the Inspector as before, as the drop down disappears.
Do i have to define the Items within the script (thus, not saving me any time at all, i may as well stick with my current method) or is there any way around this??
Many Thanks
i imagine this is a fairly noob question, but whats the best way to then define objects of a class? I have an array items to hold objects of type Item but i can't define objects in the Inspector when the class extends $$anonymous$$onoBehaviour...
I disagree that "most of your classes should extend $$anonymous$$onoBehaviour". You should only extend $$anonymous$$onoBehaviour if you're actually using it; i.e. Update and so on.
i have tried declaring all of the variables as public variables but no joy. With a normal class i can define each variable for each object (scrn3)
But when i call Equip$$anonymous$$enu[equip_selection].prefab it returns null. So to get around this i extend $$anonymous$$onobehaviour, but this happens in the Inspector (scrn2)
Answer by Eric5h5 · Jan 09, 2014 at 09:19 PM
But this requires the class to extend MonoBehaviour, i believe, in order to hold the prefab.
It does not. You can just go ahead and use GameObject in your item class.
actually, this is correct, when i tried this before i got a NullReferenceException and presumed this was caused by the class, it was however another issue. $$anonymous$$any thanks!