- Home /
Different script for each item?
I have all of these items that I am defining and I wonder if I should have a seperate script for each item because what happens is you find the item on the ground and then you press e on it to pick it up and then when you press E it adds the item to your inventory. Should I put all of the items in one script or make a seperate script for each item? Here's how it looks right now using the same script for each item: using UnityEngine; using System.Collections;
public class AllItems : MonoBehaviour
{
private const string meleeWeaponPath = "Icons/Weapons/Melee";
public static Weapon AllWeapons()
{
//MELEE WEAPONS
Weapon machete = new Weapon (12.5f, 0.0f, 4, false, 0, false);
machete.name = "Machete";
machete.description = "A fast bladed weapon";
machete.icon = Resources.Load (meleeWeaponPath + machete.name) as Texture2D;;
machete.rarity = RarityType.Common;
machete.curDurability = 150.0f;
machete.maxDurability = 150.0f;
machete.weightAmount = 1;
Weapon shovel = new Weapon (20.0f, 0.0f, 2, false, 0, false);
shovel.name = "Shovel";
shovel.description = "A blunt tool for shoveling the earth.";
shovel.icon = Resources.Load (meleeWeaponPath + shovel.name) as Texture2D;;
shovel.rarity = RarityType.Common;
shovel.curDurability = 150.0f;
shovel.maxDurability = 150.0f;
shovel.weightAmount = 1;
Weapon woodAxe = new Weapon (25.0f, 0.0f, 1, false, 0, false);
shovel.name = "Wood Axe";
shovel.description = "A sharp tool for chopping down trees.";
shovel.icon = Resources.Load (meleeWeaponPath + woodAxe.name) as Texture2D;;
shovel.rarity = RarityType.Uncommon;
shovel.curDurability = 200.0f;
shovel.maxDurability = 200.0f;
shovel.weightAmount = 1;
// BASIC RANGED/Rough Craft
//GUNS
//LASER WEAPONS
return null;
}
public static Consumable AllConsumables()
{
//FOOD
//LIQUID
//MEDICINE OR HEALTH RELATED
return null;
}
public static Armor AllArmor()
{
//BASIC AND MODERN
//ROUGH CRAFT
//POWER OR ADVANCED
return null;
}
public static Placeable AllPlaceables()
{
//BUILDINGS
//STATIONS
return null;
}
public enum ItemType
{
Armor,
Weapon,
Consumable,
Placeable
}
}
Just want to know what you guys would thing would be the best or easiest! Tell me if you have any other ideas.
Answer by HuskyPanda213 · Apr 04, 2014 at 12:54 AM
I would think it is ok as is. Just you should make allitems a static class, instead of one inheriting from MonoBehaviour.
The problem comes when you need to assign the items in the game world. How I would go about this is create a GameObject[] or whatever array. Then when you switch item, check the name of the item, and if it matches with the name of the other item, set it active, else set it inactive.
Then, that gives you the flexibility to make different scripts for different items, but only when needed. Note(about the GameObject[] Part): I would probably not make a different script for every item, I would more make the script called MeleeWeapon which is heavily customizable and so on.
Your answer
Follow this Question
Related Questions
Is there a way to shorten these? 1 Answer
error CS1526: A new expression requires () or [] after type 1 Answer
The name 'Joystick' does not denote a valid type ('not found') 2 Answers
Hotbar and item pickups? 1 Answer
Starting out C# help 2 Answers