script problems....

hey guys whats wrong with this script my script doesnt work i wanna make it so that when my 1 weapon gets destroyed by a damage reciever if its destroyed wapon 2 becomes 1 so weapon 2 is now weapon 1 i want the script to automaticaly go to the new 1 weapon so i dont have to push 6 again

function Start () {
    // Select the first weapon
    SelectWeapon(0);
}

function Update () {
    // Did the user press fire?
    if (Input.GetButton ("Fire1"))
        BroadcastMessage("Fire");

    if (Input.GetKeyDown("5")) {
        SelectWeapon(0);
    }       
    if (Input.GetKeyDown("6")) {
        SelectWeapon(1);
    }
    else if (gameObject.Destroy(gameObject);
         SelectWeapon(1);
    }   
}

function SelectWeapon (index : int) {
    for (var i=0;i<transform.childCount;i++)    {
        // Activate the selected weapon
        if (i == index)
            transform.GetChild(i).gameObject.SetActiveRecursively(true);
        // Deactivate all other weapons
        else
            transform.GetChild(i).gameObject.SetActiveRecursively(false);
    }
}

if SelectWeapon(0) is destroyed, doesn't SelectWeapon(1) automaticly change to SelectWeapon(0)?

This part it total FUBAR:

else if (gameObject.Destroy(gameObject);
     SelectWeapon(1);
}

  • The else doesn't make much sense here
  • Destroy is a function that destroys the object and it has no return value that can be compared, so it's comletely wrong inside an if statement
  • the if statement doesn't have a closing bracket
  • no semicolon after an if statement
  • you have a closing curly bracket but no opening

Beside those "little" errors if one of the weapons is gone the index of the second weapon will change since the old index-0 is gone.

There's no good way to detect if an object has been destroyed since it's gone. You should keep a seperate list of your weapons. These references turn null if the object has been destroyed.