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
0
Question by Khoa94 · May 26, 2014 at 10:59 PM · functionbooleanparametersarguments

boolean as function parameter/argument

Below is my code. I expect test to be "True" after I call "revert(test)". However, Debug.Log prints out "The value of test in GameManager is False". In the Start() function, if I just write "test = !test" then test would equal to "True". I don't know what's wrong with my code.

 #pragma strict
 
 var test : boolean = false;
 
 function revert (a : boolean)
 {
     a = !a;
 }
 
 function Start () {
     revert (test);
     Debug.Log ("The value of test in GameManager is " + test);
 }
 
 function Update () {
 
 }
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

2 Replies

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

Answer by meat5000 · May 26, 2014 at 11:01 PM

You do not return anything from your function or modify your test boolean.

test and a are not linked. Variable a exists only within the function.

 function revert () { test = !test; }

or

 function revert (a : boolean)
 {
     a = !a;
     return a;
 }

 function Start ()
 {
     test = revert (test);
     //etc
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 Khoa94 · May 26, 2014 at 11:07 PM 0
Share

Then how could I modify "test" in my "revert(a : boolean)" function? In my game, I need to write a function that takes in a boolean variable and revert it.

avatar image meat5000 ♦ · May 26, 2014 at 11:14 PM 0
Share

I gave you two ways :D

The first one changes test directly, as its a Class variable and has scope through the whole script.

The second sets up the function variable and in Start() test becomes equal to the return value (output) from the function.

avatar image Khoa94 · May 26, 2014 at 11:20 PM 0
Share

So it's not possible to pass-by-reference in Javascript?

avatar image meat5000 ♦ · May 26, 2014 at 11:26 PM 0
Share

You simply left out your 'return' which gets the output from the function and delivers it to the place the function was called. Then you need a variable to store this function output, where the function was called. I'm guessing you actually know this.

I could be wrong but JS doesn't do pointers and PBR exists through GetComponent, tenuously. But don't take my word for it; I do C and AS$$anonymous$$ mostly. All this JS/US nonsense is a lot newer to me than those :D

avatar image
0

Answer by KiraSensei · May 26, 2014 at 11:08 PM

I would write it that way :

 #pragma strict
 
 var test : boolean = false;
 
 function revert (a : boolean) : boolean {
     return !a;
 }
 
 function Start () {
     test = revert (test);
     Debug.Log ("The value of test in GameManager is " + test);
 }
 
Comment
Add comment · Show 1 · 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 Khoa94 · May 26, 2014 at 11:22 PM 0
Share

Your answer is correct but I can only vote for one and I vote for the earliest answer. Sorry that I didn't vote for you. Thanks for answering my question.

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

Using bool function with parameters 1 Answer

how to make function parameters don't need to declare (custom parameters)?? 1 Answer

How to make a game object of a certain class? 1 Answer

Check a boolean function without using Update() 1 Answer

Mecanim and trigger code 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