- Home /
Problem comparing contents of arrays
What's supposed to happen in the script below is one script is sending scriptname.BalloonArr.RemoveAt(1) which works fine and removes the 2nd item in the array called "BalloonCenter". Then the script will check to see how many items are in the array, if less than 3 it passes to check which items are left and play an animation if it's the correct remaining items. The problem is nothing is passing at the array if statement. With the script below I don't get any errors but nothing happens. If I put the animation in a called function it plays fine so i know the problem is how i'm writing the if statement. any suggestions would be great. Thanks
var BalloonArr = new Array ("BalloonLeft", "BalloonCenter", "BalloonRight");
function Update() {
var newArr = BalloonArr;
if (newArr.length < 3) {
if (newArr==["BalloonLeft","BalloonRight"]) {
animation.Play("LBalloon3_2");
}
}
}
edit: Fixed code formatting and made title more search friendly
Answer by Eric5h5 · Feb 03, 2010 at 11:46 PM
The problem is that newArr is an Array(), and you are comparing it to a String[], which is not the same thing at all. If you change the line to
var newArr = BalloonArr.ToBuiltin(String);
then it will work. Although having this in an Update function is not really a great idea, since Update runs every frame.
Your answer
![](https://koobas.hobune.stream/wayback/20220612050237im_/https://answers.unity.com/themes/thub/images/avi.jpg)