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 jvt619 · Dec 17, 2013 at 03:27 PM · buttonarraylisttry-catch

Button showing when array is full

 using UnityEngine;
 using System;
 using System.Collections;
 using System.Collections.Generic;
 
 [ExecuteInEditMode]
 
 public class Attack1 : MonoBehaviour 
 {
     private ArrayList animStrings = new ArrayList();
 
     public int maxCombo = 4;
 
     public bool ShowExecuteButton = false;
 
     void Start () 
     {
         
     }
     
     void Update ()
     {
         try
         {
             if(Input.GetKeyDown(KeyCode.Mouse1))
             {
                 Debug.Log(animStrings.Count);
                 animStrings.RemoveAt(animStrings.Count-1);
             }
         }catch(ArgumentException e){Debug.Log(e);}
     }
 
     void OnGUI()
     {
         if(animStrings.Count < maxCombo)
         {
             if(GUI.Button(new Rect(25, 25, 75, 50), "Attack"))
                 SaveAnimations("Attack");

             if(GUI.Button(new Rect(25, 75, 75, 50), "Attack01"))
                 SaveAnimations("Attack01");

             if(GUI.Button(new Rect(25, 125, 75, 50), "Attack02"))
                 SaveAnimations("Attack02");

             if(GUI.Button(new Rect(25, 175, 75, 50), "Combo"))
                 SaveAnimations("Combo");
         }
 
         if(animStrings.Count == maxCombo && !ShowExecuteButton)
         {
             if(GUI.Button(new Rect(125, 85, 125, 50), "Execute"))
             {
                 StartCoroutine(ExecuteAnimations(0));
                 ShowExecuteButton = true;
             }
         }
 
         try{
             GUI.Label(new Rect(125, 300, 100,50), animStrings[0].ToString());
             GUI.Label(new Rect(175, 300, 100,50), animStrings[1].ToString());
             GUI.Label(new Rect(225, 300, 100,50), animStrings[2].ToString());
             GUI.Label(new Rect(275, 300, 100,50), animStrings[3].ToString());
         }catch(ArgumentException e){Debug.Log(e);}
 
     }
 
     void SaveAnimations(string anim)
     {
         animStrings.Add(anim);
     }
 
     IEnumerator ExecuteAnimations(int counter)
     {
         for(int x = 0; x < maxCombo; x++)
         {
             animation.Play(animStrings[x].ToString());
             yield return new WaitForSeconds(1);
         }
 
         for(int x = 0; x < maxCombo; x++)
         {
             animStrings.RemoveAt(0);
         }
 
         ShowExecuteButton = false;
         yield break;
     }
 }
 

is this the best method to make the "Execute" button appear when the array is already full and the arraylist appears everytime i clicked a button? since there is an ArgumentException even if i'm just gonna call it in the if statement.

Comment
Add comment · Show 4
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 ziv03 · Dec 17, 2013 at 03:31 PM 0
Share

you can use Count. if(animString.Count == maxCombo)

avatar image jvt619 · Dec 17, 2013 at 04:29 PM 0
Share

wow. I feel dumb now!

btw is it okay to try-catch the GUI.label like the above code or there is another way to show it without the argumentexception?

avatar image fafase · Dec 17, 2013 at 04:36 PM 0
Share

If animString is an array then you have no reason to feel dumb because Count is not an array member but a list member. The equivalent for array is Length.

But you need to show more of your script because this is no telling us how you fill the array/list or what should be the problem.

On top of that the ArgumentException is not related to a full array, it is because you are passing an argument that is not appropriate.

avatar image jvt619 · Dec 18, 2013 at 01:38 AM 0
Share

just posted the full script.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by fafase · Dec 18, 2013 at 06:01 AM

Your problem is the type of collection you are using. Avoid ArrayList. Instead use:

  List<string> listString = new List<string>();

ArrayList stores references to object so you need to cast to the type you need, that is why you get your ArgumentException as something else is expected.

Then you do not need the try/catch.

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 jvt619 · Dec 18, 2013 at 07:37 AM 0
Share

already tried that but it still gives me an argumentexception when showing the strings of the animation. since I want to show the strings everytime the player clicks the button

EDIT: NV$$anonymous$$... just edited it using foreach and it's working great except for the position of the labels.

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

18 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

Related Questions

A node in a childnode? 1 Answer

How to make a or array of GUI.Button's? 1 Answer

How to reset the state of other buttons when clicking on one of them 0 Answers

How do we instantiate random sprites from an array... 1 Answer

Looping through Button Array and checking for OnClick 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