Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 11 Next capture
2021 2022 2023
1 capture
11 Jun 22 - 11 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Nanako · Jul 12, 2015 at 03:23 AM · parameterout

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;
 }

}

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

22 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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

Passing a parameter by reference from JavaScript 2 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges