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 $$anonymous$$ · Nov 01, 2014 at 12:07 AM · class instance

Instance of a class, adding it to a List

Hello !

I'm new to unity, but not that new to C# and I want to ask a really average and regular question which I don't know how to search after. I made a class called ConsumableItem.cs.

 using UnityEngine;
 using System.Collections;
 
 public class ConsumableItem : MonoBehaviour {
 
     public string _itemName;
     public short _manaPlus;
     public short _healthPlus;
     
     public ConsumableItem (string itemName) {
         _itemName = itemName;
         
         if (_itemName == "Gyógyital") {
             _manaPlus = 0;
             _healthPlus = 40;
         }
     }
     
     public short HealthPlus
     {
         get
         {
             return _healthPlus;
         }
     }
     
     public short ManaPlus
     {
         get
         {
             return _manaPlus;
         }
     }
 }

What I want to do is, I want to add an instance of the ConsumableItem to my Globals.cs class. My Globals.cs class looks like this:

 using UnityEngine;
 using System.Collections;
 
 public class Globals : MonoBehaviour {
     public static byte partyNumber = 2;
     public static ArrayList Items = new ArrayList();
     public static byte itemNumber = 5;
 }
 

What I want to do would look like this in C#:

 ConsumableItem a = new ConsumableItem ("Gyógyital");
 Globals.Items.Add (a);


Of Course I got the error, which I've read about at several places

You are trying to create a MonoBehaviour using the 'new' keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all

So my question is, what's the proper method for doing anything like this in unity C#?

Thanks in advance !

Comment
Add comment
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

1 Reply

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

Answer by robertbu · Nov 01, 2014 at 12:12 AM

You have two choices. The best choice given your information here is to not derive your ConsumableItem from MonoBehaviour. I don't see anything in the class that benefits from deriving it from MonoBehaviour, but this may not be the entire class. If you don't derive from MonoBehaviour, the it is just a standard C# class. You can use constructors and can create instances of the class using the 'new' operator.

Your second choice is to create your object as a component. You would do something like this:

 GameObject go = new GameObject();
 ConsumableItem a = go.AddComponent<ConsumableItem>();
 a.SetItem("Gyógyital");
 Globals.Items.Add(a);

Where 'SetItem()' is a new method that does what your constructor did in the original class.

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 $$anonymous$$ · Nov 01, 2014 at 12:23 AM 0
Share

Do I have to make it a gameObject ? What if I use it as a standard C# class? What I'd like to achieve is that in my Globals.cs there's an Item list, where I add and remove items throughout the game. When I enter the menu I want to make my items visible by looping through the Item list and getting it's name so I can later then display them in the menu and if I select it, I want to be able to get the _manaPlus and _healthPlus variables. So basically a standard inventory system.

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

1 Person is following this question.

avatar image

Related Questions

Noob question about class inheritance 2 Answers

How to declare multiple variables using a for loop in class declaration? 0 Answers

Control which class is instantiated first 1 Answer

replacing instance of class from within itself 0 Answers

Saving class data without having lots of references? 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