- Home /
How to make an array inherit from a class?
I have an array in c sharp that I want to inherit from a class in another script. How would I write this? Help
Here's the array: using UnityEngine; using System.Collections;
public class Inventory : MonoBehaviour {
public int[] weapons = new int[0];
void OnGUI(){
}
}
Here's the array:
sing UnityEngine;
using System.Collections;
public class Weapon : MonoBehaviour {
public class weapons{
public string name;
public string description;
public float damage;
public float speed;
enum Rarity{
uncommon,
common,
rare,
epic
}
}
}
If your array type is int[]
, you have an array of integers. If you array type is Weapon[]
, you have an array of weapons. Is that what you mean? This wiki article has some pretty good explanations for most questions about collections.
I mean I want to take the function weapons and put it into the array so i can use it over and over easily.
I need to take a class and none of those are classes. Can I use classes? I want to take the class weapons and put it into an array.
Answer by Nynex · Feb 11, 2014 at 03:58 AM
Well Mister NutellaDaddy, do you mean this?
Make sure to have all of this inside of one script
public class Inventory : MonoBehaviour {
public Weapon[] weapons;
void OnGUI(){
}
}
[System.Serializable]
public class Weapon {
public string name;
public string description;
public float damage;
public float speed;
enum Rarity{
uncommon,
common,
rare,
epic
}
}
Do I have to use [System.Serializable] before every class I'm using like that?
Answer by Mikejr76 · Feb 11, 2014 at 04:09 AM
Here is the Weapon class using the enumeration with a quick use case:
public class Weapon
{
public string Name { get; set; }
public string Description { get; set; }
public float Damage { get; set; }
public float Speed { get; set; }
public Rarity WeaponRarity { get; set; }
}
public enum Rarity
{
Uncommon,
Common,
Rare,
epic
};
Weapon[] weapons = {
new Weapon{ Name = "Ak-47", Damage = 10.0f, Description = "Assault Weapon", Speed = 3f, WeaponRarity = Rarity.Common},
new Weapon{ Name = "M4", Damage = 7.5f, Description = "Assault Weapon", Speed = 5f, WeaponRarity = Rarity.Common}
};
Hope it helps!