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 justify · Oct 26, 2011 at 03:19 AM · inventoryitem

Item and Inventory for a RPG game

I created a single script to be place on every gameobject tagged as item and compare the name of the gameobject and assign a value for a certain variable of the script. I'm doing this so I can have only 1 script which will identify all the items on my game.

The problem is its not working can anyone help me?

here is the script

 function Update ()

{

 if (Object.name == "Axe")

 {

     id = 0001;

 }

 if (Object.name == "Bow")

 {

     id = 0002;

 }

}

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

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Languard · Oct 26, 2011 at 04:00 AM

Well, I would expect that you are getting a compile error on this, since there is no static name property for the Unity Object class. I think what you meant to do is 'this.name', as that refers to the GameObject the script is attached to. I must admit I'm failing to see the point of the script from this tiny segment though. If you know it is a "Bow" at design time, what's the problem with setting the ID to 2 at design time?

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
avatar image
0

Answer by syclamoth · Oct 26, 2011 at 03:57 AM

How about we do this in a different way.

Define a class- 'Item', like so-

 class Item {
     var name : string = "";
     var id : int = -1;
 }

Then, make a script with nothing on it but an 'Item' var-

 var myItem : Item;

Then, when you assign this in the editor, set up its name and id manually. You only really need to do this once for each item type, because after that you can use prefabs and duplication to make the rest.

Then, when you pick up the item, given that you have it's gameObject, do this-

 var pickedUpItem : Item = gameObject.whateverYouCalledTheScriptWithTheItemInIt.myItem;

Then, you can add it to an ArrayList of Items, or whatever, to use as your inventory.

(Disclaimer- I'm not good at javascript, since I prefer to program in C#. If anyone who is better at it than I am can see any big problems with this, tell me please!)

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

A question about creating items using scriptable objects 1 Answer

Managing Item Types 1 Answer

Help making premade Items for farming game 2 Answers

Problem with dropping items from my inventory 0 Answers

Hey, i can't figure out, what my problem is. I want to Add something into the player's Inventory, but before that it should check, whether there is an Item with the same name in it, or not and if yes, if the maxAmount for that slot is reached. 0 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