I would find this helpful with a lot of things but do not know how to make one.
2 Answers
2UnityScript/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
}
Excellent answer. Glad to know this is also possible in JS. Probably worth mentioning as well that the return functions can be accessed via other scripts as well, so as to keep all the lengthy methods tucked away in a single script and reference them via GetComponent, or something similar.
– seth_slaxHey 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
could you give more detail? what are you traying to acomplish?
– BLF-Games