- Home /
Out parameter must be assigned to...
I'm getting an error with the following function: The error is: "The out parameter 'input' must be assigned to before control leaves the current method"
public void FindGrabAxisOnPart(out ObjectPart input)
{
Vector3 grabAxis = Vector3.zero;
int grabAxisInt = -1;
int upAxisInt = -1;
if (input.collider)//if it has a collider
{
grabAxisInt = ObjectPart.X_AXIS;
upAxisInt = ObjectPart.Y_AXIS;
//int biggestAxis
Vector3 size = input.collider.bounds.size;
List<float> axes = new List<float>() { size.x, size.y, size.z };
axes.Sort();
input.grabAxis = GetAxisVector(axes[axes.Count-1], size);
input.upAxis = GetAxisVector(axes[axes.Count-2], size);
input.grabRadius = GetGrabRadius(axes);
input.ZeroRotation = Quaternion.LookRotation(input.grabAxis, input.upAxis) * Quaternion.Euler(input.grabAxis * 90f);
}
}
I don't get it. It's assigned to before i even CALL the function, because i pass in a non nulled variable. This function just modifies some properties of it (It's a custom class, ObjectPart)
For what it's worth, here's the sourcecode of ObjectPart too:
public class ObjectPart { public Rigidbody body; public Collider collider; public float massProportion;//How much of the object's total mass, that this part weighs public float smallestSize;
public float grabRadius; //The length of the grab axis, in either direction, starting from the middle
public Vector3 grabAxis;
public Vector3 upAxis;
public Quaternion ZeroRotation;//The rotation which causes this object's GrabAxis to align with World Z Axis, and the upAxis to Align with World Y Axis
public const int X_AXIS = 0;
public const int Y_AXIS = 1;
public const int Z_AXIS = 2;
public ObjectPart(Rigidbody input)
{
body = input;
}
public string ToString()
{
return "b" + body + " c" + collider + " m" + massProportion + " s" + smallestSize;
}
}
Answer by supernat · Jul 12, 2015 at 04:48 AM
You shouldn't need the "out" specifier at all. "out" and "ref" are very similar with the distinction that "ref" requires that the passed object be initialized before the call is made and "out" requires that the called method set the value before it returns. Beyond that, they are pretty much the same. You really only need ref/out when you want to reassign the object in your method. If you simply want to modify values within the object, you just pass the object normally (by value, but because it is an object C# passes the reference as the value...confusing I know). Simple example:
void foo(out int i) {
// i is "out", so we have to set it
i = 5;
}
void test() {
int i;
foo(out i);
// i is 5 now
}
// whereas ref would allow this:
void foo(ref int i) {
// Empty method
}
void test() {
int i = 2; // Must be initialized for a "ref"
foo(i);
// i is still 2
}
// Now for what you want to do
void foo(Object something) {
something.i = 5;
}
void foo2(Object somethingElse) {
somethingElse = new Object();
somethingElse.i = 20;
}
void foo3(ref Object newSomething) {
newSomething = new Object();
newSomething.i = 9;
}
void test() {
Object myObj = new Object();
foo(myObj);
// myObj.i is now 5
foo2(myObj);
// myObj.i is still 5, because myObj was not replaced in foo2
foo3(myObj);
// myObj.i is now 9, and myObj is referencing the new object created in foo3
}
Hope this helps
Your answer
Follow this Question
Related Questions
Defining Parameters in a Function 0 Answers
Trouble with null object reference. 2 Answers
How to set LayerMask's optional parameter value to 'Everything'? 2 Answers
Wierd Animation Bug 0 Answers