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 winch.91 · Jan 09, 2014 at 08:13 PM · gameobjectarraysmonobehaviourclasses

Classes : extend MonoBehaviour issues

So i have the class "Item" for all the items in the game that the player will be able to interact with. I then have an array EquipMenu with items the player currently has equipped.

 public class Item
 {
     var Item_ID : int;
     var Name : String;
     var Description : String;
     var icon : Texture2D;
     var Health : float;
     var Energy : float;
     var Damage : float;
     var rarity : Rarity;
     var itemType : ItemType;
 
 }

At the moment when i need to Instantiate an "Item" i get the Item ID as follows:

var item_id : int = EquipMenu[equip_selection].Item_ID;

I then use the Item ID to select the prefab from an array of GameObjects to then instantiate.

I would much rather define the prefab in the class as follows:

 public class Item
 {
     var prefab : GameObject;
 
 }

But this requires the class to extend MonoBehaviour, i believe, in order to hold the prefab.

This then means that i cannot define the items in the Inspector as before, as the drop down disappears.

Do i have to define the Items within the script (thus, not saving me any time at all, i may as well stick with my current method) or is there any way around this??

Many Thanks

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 winch.91 · Jan 09, 2014 at 09:05 PM 0
Share

i imagine this is a fairly noob question, but whats the best way to then define objects of a class? I have an array items to hold objects of type Item but i can't define objects in the Inspector when the class extends $$anonymous$$onoBehaviour...

avatar image Eric5h5 · Jan 09, 2014 at 09:20 PM 1
Share

I disagree that "most of your classes should extend $$anonymous$$onoBehaviour". You should only extend $$anonymous$$onoBehaviour if you're actually using it; i.e. Update and so on.

avatar image winch.91 · Jan 09, 2014 at 09:23 PM 0
Share

i have tried declaring all of the variables as public variables but no joy. With a normal class i can define each variable for each object (scrn3)

But when i call Equip$$anonymous$$enu[equip_selection].prefab it returns null. So to get around this i extend $$anonymous$$onobehaviour, but this happens in the Inspector (scrn2)

alt textalt text

scrn3.png (8.4 kB)
scrn2.png (3.0 kB)

1 Reply

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

Answer by Eric5h5 · Jan 09, 2014 at 09:19 PM

But this requires the class to extend MonoBehaviour, i believe, in order to hold the prefab.

It does not. You can just go ahead and use GameObject in your item 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 winch.91 · Jan 09, 2014 at 09:33 PM 0
Share

actually, this is correct, when i tried this before i got a NullReferenceException and presumed this was caused by the class, it was however another issue. $$anonymous$$any thanks!

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

20 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

Related Questions

Need help with assigning game objects to variable 2 Answers

How to structure code for squad selection and movement 0 Answers

Accessing Monobehaviour classes on other objects 1 Answer

How do I conditionally instantiate gameobject in a public class array? 0 Answers

Select every GameObject in GameObject Array 2 Answers


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