Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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
1
Question by YoungDeveloper · Aug 14, 2013 at 04:39 AM · functioncallparameterargumentmany

Return multiple parameter

Hi, is it possible to pass two the different data types as a parameter and saving them on return ? Posting idea example.

 int number;
 bool abc;
 void function(int number, bool abc); //calling function
 //and when the function is returned, is it possible to save these variables, or just call the function ?
 
 //variables number and abc are now whats returned.
 
 public void Action(float damage,bool bleed){
    ..
    ..
    return first parameter;
    return second parameter;
 // or not return, just assign
 }

I found two posts and tried using out operator, gives couple of errors.

The out parameter `damage' must be assigned to before control leaves the current method

Hope for some ideas.

-Karl

Comment
Add comment · Show 1
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
avatar image b1naryatr0phy · Aug 12, 2015 at 11:28 PM 0
Share

From a pure program$$anonymous$$g perspective, if you find yourself in the situation where you need to return two different data types from a single function, then what you really need is to redesign your code (e.g. encapsulate those two data types into a single class.)

3 Replies

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by Wuzseen · Aug 14, 2013 at 04:53 AM

This is what structs and classes are for! You would return a struct or class that contains the relevant data.

Of course you could just make these values members of your class as well and assign the class members instead of returning an object.

Comment
Add comment · Show 4 · 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
avatar image YoungDeveloper · Aug 14, 2013 at 09:35 AM 0
Share

Hi, yes Structs and classes, i don't know how i didn't though of that. Thanks.

avatar image YoungDeveloper · Aug 14, 2013 at 10:55 AM 0
Share

Hmm, i tried passing or return struct data type and even variables itself,couldn't get it work, $$anonymous$$d posting a little example? I would really appreciate that.

avatar image meat5000 ♦ · Aug 14, 2013 at 11:04 AM 1
Share

I simply declare variables outside my functions and classes so they remain persistent through everything. This means you can change any variable in your script from in the function/class and save the return to equal a desired operation outcome or make it void.

This does have a disadvantage of constantly using more memory compared to declaring within the function/class which creates/destroys on call/exit.

avatar image Wuzseen · Aug 14, 2013 at 12:19 PM 1
Share

You would have one class, we'll call it DataClass, in one file--and it will not extend $$anonymous$$onoBehavior:

 public class DataClass {
     public first parameter;
     public second parameter

     public DataClass(first,second) {
         first parameter = first;
         second parameter = second;
     }
 }

Then in your function simply use this as your return line:

 return new DataClass(firstStuff, secondStuff);

Then you would be able to access the data like so: instance.firstParameter, instance.secondParameter

This is just pseudo code though it is a bit more C# than javascript; though the ideas work in both--you just wouldn't have the new keyword in JS.

avatar image
3

Answer by Jamora · Aug 14, 2013 at 12:37 PM

For the sake of completeness, I must point out that in imperative programming languages you can directly modify passed parameters to effectively return more than one value as a side effect.

C# has keywords, namely *out* and *ref*, that you can use to pass the reference to the parameter, instead of a copy.

As code:

 void Start(){
   int aNumber = 0;
   int secondNumber = 0;
   bool flag = PassByReference(ref aNumber , secondNumber );
   //aNumber is 5,  secondNumber is 0. flag is true.
 }
 
 bool PassByReference(ref int intByReference, int copiedInt){
    intByReference = 5;
    copiedInt = 5;
    return true;
 }

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
avatar image
0

Answer by YoungDeveloper · Aug 14, 2013 at 12:49 PM

Thank you for detailed answers.

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

20 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

Related Questions

Make Unity know which target was hit first. 1 Answer

Call a function multiple times 2 Answers

StartCoroutine important for using yield? 1 Answer

How to call a function from another script without referencing it? 1 Answer

Calling a Function from other script ( C# ) 1 Answer


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