Before writing an actual code, i would like talk about couple things, like what is overall basic idea from game creation perspective itself.
Map designers, quest creators and many others, these people mostly never code when creating maps, quests, place npc etc. They have ready custom built tools for doing that, all what they do is move sliders, search through lists and place/write (not code).
So, before actually creating the game experience itself, we need to make sure that we the right tools for doing that process, it will be taking a long time to create the map, so each helpful thing for the map designers can save tens of hours.
One way would be storing ready prefabs in one folder, but imagine if prefab amount is around 4 thousand, considering all little props, items, mobs etc. Artist will drive nuts working like that. So, custom editors is the only option, you could sort specific item lists under different categories, also show how they look or will look. That would be much more easier to work with and focus on map building than trying to find specific item for 10 minutes.
It means, each item, Quest, NPC, monster, gun, attachment, etc should be somehow recognizable (specific number or something), but how? If in simple case all what it has is a tag ?
Creating a structured list for each type of item would be good logic, for example one list for pistols, one for magazines ans so on. When the list is created you can easily access it from any point in your game and custom editors. Which means if you update you food list, your custom editor will automatically will be updated.
So basically when having these lists, you can spawn anything and anywhere And track things via Log.
So first, we create the list (array) which will hold information about the items. This script should be on player itself.
public class Items : MonoBehaviour{
//We create a Node, possible information each this current item will hold
[System.Serializable] //Without this next class "Item" wouldn't not show in inspector
public class Item{
public string name; // its name
public int weight; //just showing the idea
}
public Item[] allItems;
//This is just for showing how it might work
public InfoAboutItem(int index){ //This will be called from player
Debug.Log("You picked up " + allItems[index].name + " and it weights " + allItems[index].weight);
}
}
After attaching script you basically now can create item list in any size you want, and each can contain lots of information.
Now we need a script which will be located on item itself, so we would know what item is that.
public class ThisItem : MonoBehaviour{
public int itemIndex; // remember that first element is 0 not 1
}
Yes, that’s it :-).
Now the script which will find the item.
Also on player.
public class Player : MonoBehaviour {
void OnTriggerEnter ( Collider col ){
if (col.tag == "Item") { //All previous items in game can be under one tag "Item"
GetComponent<Items>().InfoAboutItem( col.GetComponent<ThisItem>().itemIndex );
Destroy(col.gameObject);
}
}
}
- Set the list in size of 1 (in inspector), in first element fields, enter its name and weight.
- Place ThisItem script on the pickable item, set index to 0, tag should be “Item”
After triggering the item the text will appear and it will be destroyed.
This is the basic idea how you can create very advanced item in game structures.
A bit of a lecture, but i hope it was interesting to ones just starting :).