Parameters and C# Basics

Could you guys give me a couple of examples on how to use parameters in c#, and briefly explain how that example uses them…

And at the begining of a c# script there is also this

public class PlayerController : MonoBehaviour {

Obviously with a different name and end brackets, etc… but where it says MonoBehaviour, i know you need that in order to attach you gameobject to something but ive seen people replace the MonoBehaviour part with another scripts name or just not have it at all.

What does that do?

Thanks in Advanced

If you want to associate a c# script to a game object, that script is required to inherit from MonoBehaviour.

If you saw a script that did not have MonoBehaviour as its base class, it is probably because the script was not intended to be associated with a game object but had other uses

If you saw a script that had something else than MonoBehaviour, it is probably because that something else was inheriting from MonoBehaviour (or one of it’s ancestors), or that it was not intended to be associated to a game object, just like the case above.

You can see what’s the use of MonoBehaviour in the doc.

Here is an example on how to use method paramters:

public int myMethod() {

    int myInt = getProduct(23, 47);

    return myInt;
}

public int getProduct(int numberA, int numberB)
{
    return numberA * numberB;
}

In getProduct, we specify that the method accepts two parameters that are of the int type.

Within the method, we use them like regular variables. Changing them within the method does not influences them outside the method.

The MonoBehaviour in this example is defining the base class for the PlayerController.

Basically this allows you to ‘inherit’ methods and properties from an existing class.

Think of it like this… you have a vehicle class that has a value for speed… but then you create a new class called car which has values for number of doors and wheels. However it also has a speed value that it inherited from the base vehicle class.

class Vehicle
{
    public float speed = 1.0f;

    public float GetSpeed()
    {
        return speed;
    }
}


class Car : Vehicle
{
    public int doors = 4;
    public int wheels = 4;

    public void CloseDoors()
    {
        //close door code

    }

}

class Motorbike : Vehicle
{
    public int wheels = 2;
}

so you could do

Debug.Log(carInstance.speed);
Debug.Log(motorbikeInstance.speed);
Debug.Log(motorbikeInstance.wheels);
Debug.Log(motorbikeInstance.GetSpeed());
Debug.Log(carInstance.GetSpeed());
Debug.Log(carInstance.CloseDoors());    

but not

Debug.Log(motorbikeInstance.doors); //ERROR!!!
Debug.Log(motorbikeInstance.CloseDoors());

Some general information about the concept

well for the parameters: there is always a type followed by the name of the parameter. You can add more than one parameter by separating with a comma like:
(int valueA, int valueB)

there are also out-Parameters, and you can set if they should be reference or value types with the keywords ref and value.

Or what else you like to know about parameters?