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 Magnite7 · Jan 02, 2013 at 07:16 AM · listvariableclassnotworking

class List adding to variables are assigning not adding

I have a list of a custom class I made for in my game and when I click the object it is to add the quantity amount to that list entries quantity variable but instead it is adding on to it once and won't do anything to it afterwards.

This is the quantity part of my custom class

     public class InvItem : ScriptableObject {
 public ItemType type = ItemType.Null;
 public Item item = null;
 public int quantity = 0;

 public int Quantity {
     get { return quantity; }
     set { int temp = quantity + value;
           quantity = temp; }
 }

This is how the quantity is modified when told to add the item

   public List inventory = new List();

   public void AddItem(InvItem item) {
 // i is only here for testing, testing for qauntity adding
 int i = 0;
 if (item.item != null & item.quantity > 0) {
     if (ContainsItem (item.item.id)) {
         inventory[i].Quantity = 10;    
     } else {
         if (inventory.Count < MAX_INV_SLOTS) {
             inventory.Add (item);
         }
     }
 }

}

This is how the item is Added when clicked on

 private bool canInteract;
 private InventoryManager invMan;
 private InvItem newEntry = (InvItem)ScriptableObject.CreateInstance();
 
 // Use this for initialization
 public void Start () {
     canInteract = false;
     invMan = GameObject.FindGameObjectWithTag("GameManager").GetComponent();
 }
 
 // Update is called once per frame
 void Update () {
     if (canInteract) {
         if (Input.GetButtonDown ("Interact")) {
             
             newEntry.quantity = 1; // Will be randomly generated
             
             invMan.AddItem(newEntry);
         }
     }
 }
 
 void OnMouseEnter() {
     canInteract = true;
 }
 
 void OnMouseExit() {
     canInteract = false;    
 }
 
 public void SetItem(string type, Item item) {
     newEntry.type = newEntry.SetType(type);
     newEntry.item = item;
 }

What am I doing wrong, I cannot figure it out?

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 whydoidoit · Jan 02, 2013 at 07:19 AM 0
Share

Format your code by indenting it by 4 spaces, or 1 tab before posting, or by selecting it and clicking on the 101 button. I've done it for you this time.

avatar image whydoidoit · Jan 02, 2013 at 07:23 AM 0
Share

Is your inventory list a

  List<InventoryItem> 

and that detail was lost with the code formatting issues?

avatar image StephanK · Jan 02, 2013 at 07:52 AM 0
Share

There are some syntax errors in your code. Please check if the above is the exact code in your script.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by ryba · Jan 02, 2013 at 12:15 PM

Your description of problem is rather hard to understand, but ill try to help anyway. First of all, do your code compiles ? I doubt it. If your code has compile errors, first eliminate them, then ask questions.

What you should change at first in my opinion (first line is your code, second line is what it should look like) :

 public List inventory = new List();
 public List<InvItem> inventory = new List<InvItem>();

 using System.Collections;
 using System.Collections.Generic;

 invMan = GameObject.FindGameObjectWithTag("GameManager").GetComponent();
 invMan = GameObject.FindGameObjectWithTag("GameManager").GetComponent<InventoryManager>();

 private InvItem newEntry = (InvItem)ScriptableObject.CreateInstance();
 private InvItem newEntry = ScriptableObject.CreateInstance<InvItem>();

Method SetItem(string type, Item item) seems to be bad at all. This method doesnt change your newEntry to another item instance, you change only current instance fields. It would be better if it looks like that:

 public void SetItem(string type, Item item) {
     newEntry = ScriptableObject.CreateInstance<InvItem>();
     newEntry.type = newEntry.SetType(type); // This row looks comic - but i cannot say anything else because i dont know how looks method SetType implementation
     newEntry.item = item;
 }


After all, method AddItem(InvItem item) inside manager also looks wrong. It doesnt actually add anything anywhere. Solution would look like that:

 public void AddItem(InvItem item) {
     if (item.item == null || item.quantity <= 0) {
         // parameter validation - use way with return to avoid unnecessary indents
         return; // if item is not valid, get out of this method and dont change anything
     }
     if (ContainsItem (item.item.id)) { // I fear this method is bugged, as most of your code :(
         i = 0;
         inventory[i].quantity += item.quantity; // You must get somehow index for your item, maybe its should be returned from ContainsItem ?
     } else {
         if (inventory.Count < MAX_INV_SLOTS) {
             inventory.Add (item);
         }
     }
 }


Thats good that you trying, learning, but dont expect you can really manage to finish game with your current skills, no offence - but you are making most trivial mistakes.

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

A node in a childnode? 1 Answer

Accesing custom class object from another script 1 Answer

object assigns but does not show when added to list, or any other var 0 Answers

[C#] Is there a way to call static variables directly, independent from parent class? 3 Answers

My animation is not importing into Unity. 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