#pragma strict
public var total: float;
private var count: float;

function Start(){
	count = 0;
}

function OnTriggerEnter(other: Collider){
	if(other.gameObject.CompareTag ("Pick Up")){
		other.gameObject.SetActive(false);
		count = count + 1;
		NextLevel();
	}	
}

function NextLevel(){
	if(count == total){
	Application.LoadLevel(1);
	}
	
	if(Application.loadedLevel(1) &&  count == total){
		Application.LoadLevel(2);
	}
}

I was wondering if there was a way that when I grab all of the pickups it goes to the next scene it does it for the first one but I was wondering if I could make a while loop or something so i wont have so many if statements

function NextLevel(){
if(count == total)
Application.LoadLevel(Application.loadedLevel == 1 ? 2 : 1);
}

If you want to make your code more flexible by avoiding if-else chains and switch statements, there’s two software principals you can try to employ right off the bat.

Each class should only have one responsibility

in your example you have a class that’s responsible for two things: “picking up” objects and loading new levels. Now its not really so bad for a class to have multiple responsibilities per se. In fact many games can get away with “smart” classes that multi-task, and thats perfectly fine. It just makes your code harder to manage if you try to extend the code or need reuse it somewhere else. Its just food for thought

Separate what varies from what stays the same

In general game design this one can get a little tricky if you are not sure what exactly should change and stay the same since a game’s design can constantly evolve over the course of its development. but in this case its easily apparent: “picking up elements contributes to the ending of the level, and the progress to the next.” this part always stays the same, while the other part varies “The amount you need to collect and the next you progress to differ.

There is a couple of ways you can go about doing this. It all depends on what works best with your game. A good plan would be to create a separate GameObject named “LevelManager” with a script whose sole responsibility is to control when you should go to the next level. This script will have two public variables “targetCount” and “nextLevel” which are editable in the inspector. and then a public function OnPickUp(). I would do something like make a public static variable “Instance” so that other scripts can simply access the LevelManager through that reference without having to make any expensive searching.

I never programmed Javascript for unity before so my code might not parse correctly, but you could have two classes like this:

    public class GameObjectScript extends MonoBehaviour
    {
        function OnTriggerEnter(col: Collider)
        {
             if(col.gameObject.CompareTag("Pick Up"))
            {
                col.gameObject.SetActive(false);
    
                if(LevelManager.Instance != null)
                {
                    LevelManager.Instance. OnPickUp();
                }
    
            }
    
        }
    
    }

    public class LevelManager extends Monobehaviour
    {
        public var targetCount: float;
        public var nextLevel: float;
        public static var Instance;
        private var currentCount: float;
     
        function Awake()
        {
            Instance = this;
            currentCount = 0;
        }
     
        function OnDestroy()
        {
            currentCount = 0;
            Instance = null;
        }
     
        function OnPickUp()
        {
            if(++currentCount == targetCount)
            {
                Application.LoadLevel(nextLevel);
            }
        }
    }

Now this may work for you, or your game might be using another mechanic which requires that this code would need to be altered slightly. Whichever the case may be I’m sure that keeping these principals in mind as you code can lead to a more flexible code base without tons of if’s