- Home /
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.
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?
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.
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.
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.