How do i make a function that returns a variable?

I would find this helpful with a lot of things but do not know how to make one.

UnityScript/JavaScript

function MultiplyByTwo(x : int) : int {
    return x * 2;
}

C#

public int MultiplyByTwo(int x) {
    return x * 2;
}

Example usage:

var y = MultiplyByTwo(5);
print(y); // Prints 10

Then there are more advanced ways of going about with ref and out which modify reference parameters or return multiple out parameters.

// This will reassign x to its double
public void MultiplyXByTwo(ref int x) {
    x = x * 2;
}

// This will assign y and z
public void StrangeFunction(int x, out y, out z) {
    y = x * 2;
    z = x / 2;
}

Example usage in C#:

void Start() {
    int x = 5;
    MultiplyXByTwo(ref x);
    print(x); // Prints 10

    int y; 
    int z;
    StrangeFunction(x, out y, out z);
    print(x); // Prints 10 (from previous function call)
    print(y); // Prints 20
    print(z); // Prints 5
}

Hey lenyeto,

I am not quite sure what your asking, but if its how to make a define a variable through code, it would be similar to the following…

In C#

GameObject player;

void Start () {
player = GameObject.FindWithTag("Player");
}

In UnityScript/JavaScript

var player : GameObject;

function Start () {
player = GameObject.FindWithTag("Player");
}

Hope I helped ∆ Gibson