If/else statements inside a function...

I've inserted an if/else statement inside a function..only its not working as I want it to...as in that its not working.

It works initially, meaning that the script is calling what is in the else statement, but doesn't call up the if statement when required..

This is the part of the script Im working with...

function calc () {

    var randomize = Vector3((Random.value *1) -1, (Random.value * 2) -1, (Random.value * 1) -1);
    randomize.Normalize();
    randomize *= randomness;

    if(Input.GetKeyUp("c"))
    {
        flockCenter = Controller.GetComponent("Boid Controller").flockCenter;
        flockVelocity =  Controller.GetComponent("Boid Controller").flockVelocity;
    }

    else
    {
        flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;
        flockVelocity =  Controller.GetComponent("Boid Controller Flock 1").flockVelocity;
    }

and when I press C, I get an error saying that Object reference not set to an instance of an object on this line

 flockCenter = Controller.GetComponent("Boid Controller Flock 1").flockCenter;

so it suggest that it's not looking for the new script..

What's "Controller"? is that the name of a Script component that you've written yourself? Is "Boid Controller Flock 1" the name of a gameobject in your scene that has a Controller component attached?

If so, you need something along these lines:

flockObj = GameObject.Find("Boid Controller Flock 1");
flockController = (flockObj.GetComponent(Controller) as Controller);
flockCenter = flockController.flockCenter;

Although I'm getting the feeling that you may be mixing up the concept of Components and GameObjects perhaps, so if you could give a little more detail about the hierarchy and naming of your gameobjects and components, this would help a lot in solving your problem.

If you use GetComponent(something) and you don't know if the object exists anymore it's always good to use an if after that controlling if it does exist, like this:

boidController = Controller.GetComponent("Boid Controller");
if (boidController != null) {
    flockCenter = boidController.flockCenter;
}

And just to mention that GetComponent calls is quite CPU heavy, so it's good to cache values when possible, to use less CPU, first get the Boid controller, save it to a variable, and then extract the values:

if(Input.GetKeyUp("c"))
{
    boidController = Controller.GetComponent("Boid Controller");
    if (boidController != null) {
        flockCenter = boidController.flockCenter;
        flockVelocity = boidController.flockVelocity;
    }
}

Not seeing the rest of the code makes diagnosing this a bit tricky. But I strongly suspect the problem isn't with your if-statement but with how you are handling input.

For example, let's say you call calc() in response to a key being pressed. Input.GetKeyUp will return false because the C key wasn't just released.

Doublecheck the conditions that lead to calc() being called and make sure they match the Input check inside of calc(). Better yet, change the code so it doesn't matter. One way would be for the code which is responding to the key/mouse/whatever to pass an argument to calc() that tells it what to do. For example, it could pass in the name or index of which boid controller to switch to.

Instead of putting "c" as your key put KeyCode.C

Instead of putting "c" put KeyCode.C as Bailey said.