Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by NutellaDaddy · Feb 11, 2014 at 03:22 AM · arraysinheritanceclasses

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
         }
     }
 }
 
Comment
Add comment · Show 3
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image rutter · Feb 11, 2014 at 03:24 AM 0
Share

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.

avatar image NutellaDaddy · Feb 11, 2014 at 03:43 AM 0
Share

I mean I want to take the function weapons and put it into the array so i can use it over and over easily.

avatar image NutellaDaddy · Feb 11, 2014 at 03:49 AM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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
        }
    
 }
Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image NutellaDaddy · Feb 11, 2014 at 12:45 PM 0
Share

Do I have to use [System.Serializable] before every class I'm using like that?

avatar image
0

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!

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Can I preserve the values in a parent class when I overwrite it with a child class? 1 Answer

Inheritance from object to object 1 Answer

How do I write this C# snipet in java script? 1 Answer

Problem using variable of type CustomStat to indicate which player statistic to change 1 Answer

Classes : extend MonoBehaviour issues 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges