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 xhybridxreflex · Jan 23, 2013 at 09:59 AM · javascriptplayerinventory

How to add an inventory to the Player

Hey there, I'm trying to create an inventory for my player. I created this short javascript code and didn't seem to work. Any ideas? I've tried fixing the errors but only leading to more. Please help!

 public class Inventory
 
     List items = new List();
     InventoryItem selectedItem;
     Player owner;
     int currentlySelected = 0;
     public void Init(Player newPlayer)
     {
          newPlayer = owner;
     }
     public bool AddItem(InventoryItem newItem)
     {
          if(owner.strength >= GetTotalMass() + newItem.mass)
          {
               items.Add(newItem);
               return true;
          } else {
              return false;
          }
     }
     public void DropItem(InventoryItem newItem)
     {
         items.Remove(newItem);
     }
     public float GetTotalMass()
     {
          float mass = 0;
          foreach(InventoryItem it in items)
          {
              mass += it.mass;
          }
          return mass;
     }
     public InventoryItem DrawItemsGrid(int columns)
     {
           Texture2D[] textures = GetTextures();
           currentlySelected = GUILayout.SelectionGrid(currentlySelected, textures, columns);
           return items[currentlySelected];
     }
     public Texture2D[] GetTextures()
     {
          List textures = new List();
          foreach(InventoryItem it in items)
          {
              textures.Add(it.icon);
          }
          return textures.ToArray();
     }
 }
 
 public class Player : MonoBehaviour
 {
     public float strength;
     public Inventory invent;
     public Hat myHat;
     public Sword mySword;
     public Sword myOtherSword;
     void Start()
     {
          invent.Init(this);
          invent.Add(myHat);
          invent.Add(mySword);
          invent.Add(myOtherSword);
     }
     public void TipHat()
     {
         //play a hat-tipping animation!
     }
     public void Attack()
     {
         // swing a sword
     }
     void OnGUI()
     {
          InventoryItem currentItem = invent.DrawItemsGrid(2);
          currentItem.DrawGUIInfo();
          if(GUILayout.Button("Activate " + currentItem.itemName))
          {
                currentItem.Activate(this);
          }
          if(GUILayout.Button("Drop " + currentItem.itemName))
          {
                invent.DropItem(currentItem);
          }
     }
 }
 
 public abstract class InventoryItem
 {
     public string itemName;
     public string dragInfo;
     public float mass;
     public Texture2D icon;
     
     public abstract void Activate(Player player);
     public void DrawGUIInfo(){ GUILayout.Label(dragInfo); }
 }
 
 [System.Serializable]
 public class Hat : InventoryItem
 {
     void Activate(Player player)
     {
         player.TipHat();
     }
 }
 
 [System.Serializable]
 public class Sword : InventoryItem
 {
     void Activate(Player player)
     {
         player.Attack();
     }
 }

[Edit by Berenger : formatting. Please next time select your code and hit the 101 010 button.]

Comment
Add comment · Show 1
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 ryba · Jan 23, 2013 at 11:06 AM 0
Share

If u want help, fix your message (format your source), its painfull to read that code. Also say anything whats wrong, copy errors, or describe whats going on ... we are not $$anonymous$$d readers ...

2 Replies

· Add your reply
  • Sort: 
avatar image
0

Answer by Berenger · Jan 23, 2013 at 11:16 AM

At first look, your gui stuff isn't in an area and you're using guilayout. The rest seems ok.

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 ryba · Jan 23, 2013 at 11:45 AM

First of all, code you provided isn't javascript, but C#. In that case you must put that code in C# files, not javascript files. Thats probably 90-100% of your errors.

Also dont mix your scripts, decide to use either only javascript or only C#. Mixing javascript and C# is problematic, it can be done, but require experiance you lack of.

Another issue - avoid putting multiple classes in one single file.

Also what i've noticed - it seems you didnt instantiate Inventory class in your Player component. Do that:

 void Start() {
      invent = new Inventory();
      invent.Init(this);
      invent.Add(myHat);
      invent.Add(mySword);
      invent.Add(myOtherSword);
 }

Hard to say whats also wrong, please apply my advices and ping us whats the results, then if it will still be wrong, we could move one.

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

11 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

Related Questions

Script Not Working 0 Answers

Pause online game 1 Answer

Trouble setting up an inventory system to interact with game objects. 2 Answers

Move RigidBody character relative to camera. 2 Answers

Item pickup add to inventory? 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