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 youngapprentice · Apr 21, 2013 at 09:55 PM · parametereasy

Defining Parameters in a Function

Hi, all! I am not new to giving functions parameters to wrk with, but I was wondering if, besides using a return, I can define a parameter.

Here's an example:

I have a timer. When the timer turns off, I want a bool to turn from false to true. I would like to just be able to call Timer(), and supply it any old boolean I want, and have it toggle it when the timer finishes.

A timer would be a super simple function, but I only want it to return once it finishes.

The problem I see with the return function is this:

I have a timer that counts down three seconds and returns true.

In the start function I have myBool = Timer(3);

Well I don't know if the timer will set the bool to true after three seconds, or if it will be disregarded simply because Start is only checked in the beginning.

Sorry for being so unclear. I hope that maybe I have cleared any confusion with examples.

Thanks! - YA

Comment
Add comment · Show 11
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 whydoidoit · Apr 21, 2013 at 09:58 PM 1
Share

What language?

avatar image whydoidoit · Apr 21, 2013 at 10:14 PM 1
Share

Well in this case they are. You cannot use "reference" ref parameters in Unity Script.

In C#:

    IEnumerator Timer(float seconds, ref bool value)
    {
           yield return new WaitForSeconds(seconds);
           value = true;
    }


    StartCoroutine(Time(3, ref myBool));
avatar image Eric5h5 · Apr 21, 2013 at 11:21 PM 1
Share

The workaround is to use a class, since classes are always passed by reference.

avatar image Bunny83 · Apr 22, 2013 at 12:08 AM 2
Share

Btw, as far as i remember generators / iterators can't have ref or out parameters since they are internally classes and the parameters become member variables. So it's not possible to use a ref or out parameter. Passing a class reference will of course work.

avatar image Eric5h5 · Apr 22, 2013 at 04:18 AM 1
Share

So I would make the timer a class function?

No, if you wanted to pass a boolean as a reference in JS, you'd make a class that just contains a boolean, and use that as the boolean ins$$anonymous$$d of a "real" boolean.

Show more comments

0 Replies

· Add your reply
  • Sort: 

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

14 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

Related Questions

Changing Color Based on Z position 2 Answers

Why pass parameters into a function? 2 Answers

How can i make my cube not be a 3D parallelogram when i rotate it under its parent? 1 Answer

Local Variables 2 Answers

Another collision detection issue 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