• 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 Mubanga · Aug 25, 2014 at 04:01 PM · c#voidassignment

Custom Inventory system: void argument not assigned properly?

I'm working on an inventory system and this script is for the equip part of that system. I have five slots to equip items and they work if I equip stuff manually. But now I am trying to write a void to equip them a bit easier. To get there I have this setup:

 void Update () {
         if (Input.GetButtonDown("Up")) {
             equipItem (0, ItemType.Cap, cap);
             print (cap.itemID);
         }
 }



and the void it self:

 public void equipItem (int id, ItemType type, Item slot) {
     for (int i = 0; i < database.items.Count; i++) {
         if (database.items[i].itemID == id && database.items[i].itemType == type) {
             slot = database.items[i];
             print (cap.itemID);
             print (slot.itemID);
         }
     }
 }

Print (cap.itemID) returns as -1 (an empty Item) slot.itemID return as 0 (the ID that it is supposed to be). Did I make a mistake assigning the void arguments or is what I am trying to just dumb, and not possible/not the way to do it?

UPDATE: So the problem is that the Item "slot" get's changed but not the Item "cap" that should have been there in place of the Item "slot". I got it working by changing the whole void in to an new item, this way i could say cap = equipItem ( 0, itemTyp.Cap); and get what I want for now, problem is I also want to be able to acces this from other scripts.

UPDATE2: Never mind I can just find this script by tagging the object it is attached to.

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
0
Best Answer

Answer by FlaSh-G · Aug 25, 2014 at 05:01 PM

Paramters are local variables. Whatever you do to the variable slot within equipItem will not matter outside of it. You can return a value and assign it to "cap" doing this:

 cap = equipItem (0, ItemType.Cap);

and

     public Item equipItem (int id, ItemType type)
     {
       Item slot = null;
       for (int i = 0; i < database.items.Count; i++)
       {
         if (database.items[i].itemID == id && database.items[i].itemType == type)
         {
           slot = database.items[i];
           print (cap.itemID);
           print (slot.itemID);
         }
       }
       return slot;
    }
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 Mubanga · Aug 25, 2014 at 05:12 PM 0
Share

Yeah, I figured that out myself as well (it is in the update). Thanks anyway!

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.

Welcome to Unity Answers

The best place to ask and answer questions about development with Unity.

To help users navigate the site we have posted a site navigation guide.

If you are a new user to Unity Answers, check out our FAQ for more information.

Make sure to check out our Knowledge Base for commonly asked Unity questions.

If you are a moderator, see our Moderator Guidelines page.

We are making improvements to UA, see the list of changes.



Follow this Question

Answers Answers and Comments

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer

C# Return Type Error? 1 Answer

Start a Coroutine in script A, from script B C# 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