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 madmike6537 · Dec 14, 2012 at 05:26 AM · getcomponentinventoryinheritance

Inventory / inhertance driving me insane - help with theory please.

Ok, I am trying to make an inventory system and I am going in circles. I think if I add one more GetComponent call into my code my computer might burst into flames immedietely. I thought about just stealing one but I really want to understand this. This thread isnt really about how to make the actual OnGUI or anything its about the inheritance and setting up the scripts and the code.

I am not asking for code I just need to understand the flow, and get it through my apparently thick head.

So what I have so far is this:

First I have a script(class) called Item. This script outlines the basic features of an item: Name, cost, description. Ok so far so good.

If I understand correctly, I can then have other scripts, weapon for example, that inherit from item and further give more qualities of the item - damage, for example. The item then also inherits the basic traits, cost, name description which I set in the item script I believe? Ok I get that I think.

So now, I have a script called "Item Manager" which is where I create my list of Items: `public List PlayerInventory = new List();` And so far I have been putting the functions in here for adding to this list. So for example, if I click a button or something it adds a new Item to the inventory list and sets the parameters: Basically like this:

 Item newItem = new Item();
 
 newItem.name = new ItemName; // Declared at top of this script.
 //etc
 //etc
 
 PlayerInventory.Add(newItem);

I guess this is where I get lost:

My biggest question is this: If I have a gameObject in my scene that has a script on it that does a bunch of stuff, how can I set this to be of a type weapon, for example or of type Armor? For example, I have an arrow that gets fired in my scene by the player. It has a script that does a bunch of stuff so I feel like I cant make it inherit from the Item script or all that wont work(because its not inheriting from mono). How do I set it to be of type: Item or type: weapon?

When I then click on the gameObject, how can I tell what kind of object it is? I guess that depends on the answer to the above. I "think" if I can somehow make it inherit from "item" I can use gameobject.GetType I believe? If I cant inherit though I have no idea. I tried to set a variable string called type on the object and then access that but thats making no sense and obviously the wrong way to go about it.

How would you recommend adding gameObjects into the List(Player Inventory)? Right now I have a empty gameobject which holds the script ItemManager which adds the items. Should I just access those AddItem functions using GetComponent to add the Item when my player clicks on an object? Or should I put the script on the player themselves? I see everyone using the empty gameobject with a "inventory manager" of sorts and I am not sure why they go this direction.

Whew I am rambling here I guess I just need some direction on how to manage all this! Feel free to direct me to a link that describes this flow but I havent found anything yet :(

Comment
Add comment · Show 4
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 Fattie · Dec 14, 2012 at 08:17 AM 0
Share

$$anonymous$$ad$$anonymous$$ike, massively step back and relax :)

learn the basics of finding other components etc -- what about unityGE$$anonymous$$S.com for a massive intro

You mention that you "use getcomponent a lot" - well that's normal, i use it in every line of code.

there is a caveat to that - once you know what you're doing, just GET A REFERENCE TO that other item, and then use that reference.

"$$anonymous$$y biggest question is this: If I have a gameObject in my scene that has a script on it that does a bunch of stuff, how can I set this to be of a type weapon, for example or of type Armor"

just learn about ENU$$anonymous$$, it is very simple

"how can I tell what kind of object it is?"

it would be nothing more than a variable called "WhatTheFuckAmI" ? and that would be an ENU$$anonymous$$. learn about ENU$$anonymous$$ immediately (it is dead simple)

regarding your second last paradgraph I really don't understand it at all, but in a word just use an ARRAY. (in fact -- just to confuse you never for any reason LITERALLY use an array in unity, just use the "List" form - if you don't know about that look it up. again unityGE$$anonymous$$S.com)

hope it helps in some way

avatar image Tourist · Dec 14, 2012 at 10:17 AM 0
Share

Actually he wants inheritance because the class Weapon has more parameters than the class Item.

let me be sure of something : your inheritance of item would be like this? $$anonymous$$onoBehaviours <- Item <- Weapon <- Arrow

If so, when you get the gameobject, use GetComponent (c# syntax) to get the first component of type item. Then use component.GetType() == typeof(Weapon) to check if it's a weapon for example.

avatar image madmike6537 · Dec 14, 2012 at 02:57 PM 0
Share

Hi and thank you both for the answers. I will look into enums. Tourist you are correct that on the inheritance - mono - item - weapon - arrow just as an example. But I can't inherit arrow from weapon can I or won't all my update functions and what not stop working in my arrow script? Or maybe because item originally comes from mono it will be ok?

avatar image madmike6537 · Dec 14, 2012 at 03:00 PM 0
Share

Tourist on your last paragraph of I understand correctly you are saying when I get the game object as in I click on it with on mouse down or something. Then I can use if GameObject get type == type of? That's what I was thinking but it will only work of o inherit from weapon. Right? And I guess that's where I am not sure if I can do that or not.

1 Reply

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

Answer by Tourist · Dec 14, 2012 at 03:40 PM

A gameobject is a gameobject. A component is something you can add to a gameobject to add some behaviours. This is the part you can control, and use inheritance on. Here is a sample of code. The function 'GetYourObjectOnCLick' is just there to simulate the way you are getting the gameobject.

 GameObject obj = GetYourObjectOnCLick();
 Item item = obj.GetComponent<Item>();
 
 if(item.GetType() == typeof(Weapon))
 {
 // do your stuff there
 }
 else if(item.GetType() == typeof(Ammo))
 {
 // do other stuff there
 }
 else
 {
 // do something else
 }
Comment
Add comment · Show 13 · 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 madmike6537 · Dec 14, 2012 at 03:46 PM 0
Share

Ok makes sense but wouldn't i have to have the item script attached to my GameObject for this to work? What if I have the weapon script attached, will it still work since weapon inherits from item?

avatar image Tourist · Dec 14, 2012 at 03:55 PM 0
Share

That's the purpose of inheritance. If you attach the weapon script to your gameobject, then you don't have to attach item as well because Weapon is already an Item (due to inheritance).

I suggest you make some research on your own about inheritance in any program$$anonymous$$g language. This will help you to structure your code. Here is the article about inheritance on Wikipedia : http://en.wikipedia.org/wiki/Inheritance_%28object-oriented_program$$anonymous$$g%29

But you can find others explanations.

avatar image madmike6537 · Dec 14, 2012 at 04:09 PM 0
Share

Thanks again I marked as answered. Sounds like I have some more reading to do :)

avatar image Bunny83 · Dec 15, 2012 at 01:21 AM 0
Share

You simply need this inheritance chain:

 Item --inherits from--> $$anonymous$$onoBehaviour
 Weapon --inherits from--> Item
 Ammo --inherits from--> Item
 A$$anonymous$$47 --inherits from--> Weapon  // just an additional example


therefore a Weapon is also an Item and also a $$anonymous$$onoBehaviour.

$$anonymous$$eep in $$anonymous$$d that when Item is derived from $$anonymous$$onoBehaviour it can't be created with new like you did in your question. It is a component and always have to be attached to a GameObject. But since it's an item in the game this makes perfectly sense.

avatar image madmike6537 · Dec 15, 2012 at 04:36 AM 0
Share

Hmm. Interesting because ya I have been getting a warning that I can't use new when it inherits from mono. I guess that's why. So if I have a script for an item call arrow, that tells it to move forward and all that, and inherits from mono, would it be better to just create a new script that inherits from item and attach that to my arrow prefab that way my arrow is of type item?

Ins$$anonymous$$d of just trying to add in the new command into my existing arrow script?

Show more comments

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

12 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

Related Questions

How can I access an inherited method from a separate (collided) object? 1 Answer

Inventory Management Issue 2 Answers

Inheritance vs RequireComponent -1 Answers

C# - How do you GetComponent of a child class that inherits from a specific class? 1 Answer

Organise scripts with the same name? 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