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 TajamSoft · Jan 28, 2015 at 07:34 AM · variablesinventory

Weird issues when adding an object to a variable.

Hey there! Today, i have started to work on a project, everything is working perfectly, but the inventory system.

I have gameobjects with an script attached, and another gameobject which is the player. The player has a script named "invManager" which contains all the code required for managing the inventory during the play. On the object i'm willing to pick up, i have another object attached, with an script which calls the invManager on the player to get himself added to the inventory list, then, the inventory does a quick check to see what slot is free, and places the pickable object there, but there seems to be a weird issue:

Whatever i do, the object gets attached to the slot 9 (the last one), but the real free slot is 1...

Here is the code on the pickable object (It does not destroys himself yet):

 using UnityEngine;
 using System.Collections;
 
 public class InputBlock : MonoBehaviour {
 
     public Color highColor;
     private Color startcolor;
     private bool showGUI = false;
     public GameObject mouseObject;
     public mouseManager mouseScript;
     public invManager invScript;
 
     void Start()
     {
         mouseScript = mouseObject.GetComponent<mouseManager>(); 
         invScript = mouseObject.GetComponent<invManager>();
     }
     void OnMouseEnter()
     {
         startcolor = renderer.material.color;
         renderer.material.color = highColor;
 
 
     }
     void OnMouseExit()
     {
         renderer.material.color = startcolor;
     }
 
     void OnMouseDown()  //TODO: Make a GUI (Not needed yet)
     {
         invScript.checkFreeSlots ();
         invScript.addBlock(this.gameObject);
 
 
     }
 }

And the invManager code:

 using UnityEngine;
 using System.Collections;
 
 public class invManager : MonoBehaviour {  //Script to manage inventory, and placement of blocks.
 
     //Slot definitions (Starts by 1)
     public GameObject slot1 = null;
     public GameObject slot2 = null;
     public GameObject slot3 = null;
     public GameObject slot4 = null;
     public GameObject slot5 = null;
     public GameObject slot6 = null;
     public GameObject slot7 = null;
     public GameObject slot8 = null;
     public GameObject slot9 = null;
     //End of slot definition
 
     public int freeSlot = 1; //Used to easily tell other scripts which slot to place stuff at
 
 
     void Start () {
         slot1 = null;
         slot2 = null;
         slot3 = null;
         slot4 = null;
         slot5 = null;
         slot6 = null;
         slot7 = null;
         slot8 = null;
         slot9 = null;
         print ("Inventory system initialized");
 
 
 
     }
     
 
     void Update () {
 checkFreeSlots();
         print (freeSlot);
     }
     public void checkFreeSlots(){  //Barebones function to check slots
         //The smaller slot is the chosen.
         if (slot1 == null) {
             freeSlot = 1;
             return;
         }
         if (slot2 == null) {
             freeSlot = 2;
             return;
         }
         if (slot3 == null) {
             freeSlot = 3;
             return;
         }
         if (slot4 == null) {
             freeSlot = 4;
             return;
         }
         if (slot5 == null) {
             freeSlot = 5;
             return;
         }
         if (slot6 == null) {
             freeSlot = 6;
             return;
         }
         if (slot7 == null) {
             freeSlot = 7;
             return;
         }
         if (slot8 == null) {
             freeSlot = 8;
             return;
         }
         if (slot9 == null) {
             freeSlot = 9;
             print ("Chosen 9");
             return;
         }
         
         
     }
     public void addBlock(GameObject blockObject)  //For some reason, always adds to slot 9
         {
         checkFreeSlots ();
         print (freeSlot);  //Debug (Always prints 9 for some reason...
         print ("[INV] Adding block...");
         if (freeSlot == 1) {
             slot1 = blockObject;
             print ("[INV] Success! Added Block to slot:"+freeSlot);
             return;
         }
         if (freeSlot == 2) {
             slot2 = blockObject;
             print ("[INV] Success! Added Block to slot:"+freeSlot);
             return;
         }
         if (freeSlot == 3) {
             slot3 = blockObject;
             print ("[INV] Success! Added Block to slot:"+freeSlot);
             return;
         }
         if (freeSlot == 4) {
             slot4 = blockObject;
             print ("[INV] Success! Added Block to slot:"+freeSlot);
             return;
         }
         if (freeSlot == 5) {
             slot5 = blockObject;
             print ("[INV] Success! Added Block to slot:"+freeSlot);
             return;
         }
         if (freeSlot == 6) {
             slot6 = blockObject;
             print ("[INV] Success! Added Block to slot:"+freeSlot);
             return;
         }
         if (freeSlot == 7) {
             slot7 = blockObject;
             print ("[INV] Success! Added Block to slot:"+freeSlot);
             return;
         }
         if (freeSlot == 8) {
             slot8 = blockObject;
             print ("[INV] Success! Added Block to slot:"+freeSlot);
             return;
         }
         if (freeSlot == 9) {
             slot9 = blockObject;
             print ("[INV] Success! Added Block to slot:"+freeSlot);
             return;
         }
             
 
     }
 
 
 }
 

The results of clicking on the pickable object: "Success! Added block to slot 9" And i'm specting: "Success! Added block to slot 1"

No idea why this happens... Thanks for your help!

Cheers!

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

Answer by nu-assets · Jan 28, 2015 at 08:31 AM

I would suggest an iterative approach (makes your script shorter and more clear):

 using UnityEngine;
 using System.Collections;
 
 //Script to manage inventory, and placement of blocks.
 public class InvManager : MonoBehaviour
 {  
     public bool Full
     {
         get
         {
             return GetNextFreeSlot() < 0;
         }
     }
 
     //Slot definitions (Starts by 1)
     public GameObject[] Slots = new  GameObject[9];
 
 
     void Start()
     {        
         print("Inventory system initialized");
     }
 
     public int GetNextFreeSlot()
     {  
         //The smaller slot is the chosen.
         for (int i = 0; i < Slots.Length; i++)
         {
             GameObject go = Slots[i];
             if (go == null)
             {
                 return i;
             }
         }
 
         // Return -1 if no empty slot found
         return -1;
     }
 
     // Adds a block to the inventory is not full
     public void AddBlock(GameObject blockObject)  
     {
         if (!Full)
         {
             int slotIndex = GetNextFreeSlot();
 
             print("[INV] Adding block @ " + slotIndex);
 
             Slots[slotIndex] = blockObject;
         }
         else
         {
             print("[INV] Inventory is full...");
         }
     }
 }
 

And call it like this:

     void OnMouseDown() 
     {
         invScript.AddBlock(this.gameObject);
     }

But keep in mind that the instance 'invScript' should be globally available during runtime (singleton, static, ...).

I have also refactored your script to fit the common c# conventions.. ;)

Comment
Add comment · Show 2 · 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 sniper43 · Jan 28, 2015 at 09:02 AM 0
Share

Well put together (you have my upvote), but the only shitty part is that we still don't know why the value assigned was 9.

avatar image nu-assets · Jan 28, 2015 at 09:13 AM 0
Share

I have removed all unnecessary calls to 'checkFreeSlots' (GetNextFreeSlot) and suggested a durable hold of the data. This makes it easier to find the buggy part. If that would not work, there must be another thing causing this misbehaviour.

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

Index out of range exception 0 Answers

Inventory Cursor Location 1 Answer

Using PlayerPrefs to Imitate a players inventory 1 Answer

GUI.Button is acting funky. 2 Answers

How to manage inventory 'objects' in C# for behind the scenes inventory stuff 4 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