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 /
This question was closed Aug 15, 2014 at 06:16 PM by stuart6854 for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by stuart6854 · Jan 06, 2014 at 11:35 AM · counterprintstrangeiterateswitch-case

Count Variable Acting Strange

CODE:

 public void InventoryWindow(int id){
         int cnt = 0;
         string itemToolTip;                        
         for(int y = 0; y < _inventoryRows; y++){
             for(int x = 0; x < _inventoryCols; x++){
                 if(cnt < inv.Stacks.Count && inv.Stacks[cnt] != null){
                     if(!editing){
                         itemToolTip =  "<b>" + inv.Stacks[cnt].Item.Name + "</b> \n " + inv.Stacks[cnt].Item.Description + " \n <size=10>" + inv.Stacks[cnt].Item.Type + "</size>";
                     } else{itemToolTip = "";}
                     if(GUI.Button(new Rect(5 + (x * buttonWidth), 20 + (y * buttonHeight), buttonWidth, buttonHeight), new GUIContent(inv.Stacks[cnt].Item.Icon, itemToolTip), "box")){
                         editing = true;
                     }
                     if(editing){
                         GUI.BeginGroup(new Rect(32.5f, 280, 200, 90), comboBacker);
                         for(int i = 0 ; i < items.Length; i++){
                             int index = cnt;
                             print(cnt + " = Index!");//Normal here********
                             if(GUI.Button(new Rect(0, (30 * i), 200, 30), items[i], "button")){
                                 print(cnt + " = COUNT!!!!");//Always Zero here********
                                 selectedItem = items[i];
                                 editing = false;
                                 switch(selectedItem){
                                 case "Use":
                                     //print ("Use Item");//Use Item
                                     UseItem(inv.Stacks[index].Item);
                                     break;
                                 case "Drop":
                                     //print ("Drop Item");//Drop Item
                                     break;
                                 case "Cancel":
                                     //Do nothing, just close options
                                     break;
                                 default:
                                     //Do nothing, just close options
                                     break;
                                 }
                             }
                         }
                         GUI.EndGroup();
                     }
                     
                     GUI.Label(new Rect(6 + (x * buttonWidth), 20 + (y * buttonHeight), 50, 50), inv.Stacks[cnt].Size.ToString(), "label");
                     //print (inv.Stacks[cnt].Item.Name);
 
                 } else if(inv.Stacks[cnt] == null){
                     GUI.Button(new Rect(5 + (x * buttonWidth), 20 + (y * buttonHeight), buttonWidth, buttonHeight), (x + y * _inventoryCols).ToString(), "box");
                 }
                 //print(cnt);
                 cnt++;
             }
         }
         Tooltip.Instance.TooltipGUI();
         GUI.DragWindow();
     }


For some reason when i try to reference the 'cnt' variable at the switch statement it always equals ZERO?

About 4/5 lines from bottom i have a print which is working fine and the 'cnt' variales is working, but why not for the switch statement?

EDIT:

The 'cnt' variable stops working after line 18, but works fine before it. *Added Whole Function

Comment
Add comment · Show 12
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 HappyMoo · Jan 06, 2014 at 11:54 AM 0
Share

you don't reference the cnt variable at the switch statement. Also, you don't show where you define the variable. There are no Debug.Logs etc. Have you done some basic debugging like Debug-Log it, to seee where it changes? if so, you should have a line on which it still works and a next line where the value is wrong. Please find out

avatar image UnbreakableOne · Jan 06, 2014 at 12:11 PM 0
Share

on the same road as stuart6854, I would check the cnt in the first if statement. I'm afraid that there is not enough code because we can't see where cnt comes from, it's maybe null or zero from the init.

I suggest you put some breakpoints and check the value and track along the code.

If that didn't help, please provide more code but that might not help that much because it seems coupled with some other parts.

Cheers.

avatar image HappyMoo · Jan 06, 2014 at 12:23 PM 0
Share

Well, that is strange. Have you tried attaching to unity and debugging it step by step?

avatar image stuart6854 · Jan 06, 2014 at 12:24 PM 0
Share

'Attaching to unity'

What do you mean?

avatar image HappyMoo · Jan 06, 2014 at 12:59 PM 0
Share

http://www.youtube.com/watch?v=-D6qXaGSC2k

Show more comments

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by stuart6854 · Jan 06, 2014 at 04:43 PM

OK, i have worked out the silly problem :P

It was because i was using 'cnt' to reference the item which was ever changing while the user selected a drop down box option(Dunno why it was always 0 though?), so when the userclicked on the inventoryitem i needed to store the current index to stop it changing.

Thanks anyway :P

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

Follow this Question

Answers Answers and Comments

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

Related Questions

Function with a variable text in the middle of it 1 Answer

Finding Closest Enemy... 2 Answers

Way to print and email from Unity application? 1 Answer

Unity's Equivalent of Console.Write(); 1 Answer

print the mass values of rigidbody attached with the gameobjects assigned to the array variable 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