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 Catlinman · Dec 27, 2012 at 03:03 PM · getcomponentbooleanreturn

Return a boolean from a function

Sorry that I didn't fully explain what I am trying to do. When I wrote all my scripts I would use static vars but I now finally noticed that it is not a good thing to use them as they are not object dependant. I rewrote all affected scripts to use functions that when called from another component return a private variable which would otherwise be static. All the variables in a component on a gameobject have their own values which can only be changed if the script in the component is called which to directly changes the value.

The code:

 //DroneFunctions.js
 //Holds health, power and movement state info
 
 private var droneActive : boolean;
 private var droneHealth : float;
 private var droneAlive : boolean = true;
 private var movementEnabled : boolean;
 
 //Functions to be called from other components to access the values in this one.
 
 function getDroneActive(){
     return droneActive;
 }
 
 function getDroneHealth(){
     return droneHealth;
 }
 
 function getDroneAlive(){
     return droneAlive;
 }
 
 function getDroneMovementEnabled(){
     return movementEnabled;
 }
 
 //Functions to be called from other components to change the values in this one.
 
 function setDroneActive(VALUE : boolean){
     droneActive = VALUE;
 }
 
 function setDroneHealth(VALUE : float){
     droneHealth = VALUE;
 }
 
 function setDroneAlive(VALUE : boolean){
     droneAlive = VALUE;
 }
 
 function setDroneMovementEnabled(){
     movementEnabled = VALUE;
 }
 
 //DroneMovement.js
 //Tracks input and adds movement to the drone
 
 function Update(){
     if(gameObject.GetComponent(DroneFunctions).getDroneMovementEnabled()){
         //Track input
         //Do movement
     }
 }

Both components are attached to the same gameObject. The problem is that whenever I call 'getDroneMovementEnabled' it returns the boolean but then show an error of: 'It is not possible to invoke an expression of type 'boolean'.'

ORIGINAL:

Hey guys,

I would like to call a function and make it return a boolean value. I then want to use the value in an if statement and check if it is true. So far I have been getting an error of:

It is not possible to invoke an expression of type 'boolean'.

The code:

 //Health Component attached to object A

 private var isAlive : boolean = true;

 function getAlive(){
     return isAlive;
 }
 
 //CheckHealth Component attached to object B
 
 function checkHealth(){
     if(Camera.main.transform.parent.gameObject.GetComponent(Health).isAlive()); //do something awesome
 }

EDIT: I forgot to add the parentheses after isAlive

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 Dave-Carlile · Dec 27, 2012 at 04:13 PM 0
Share

Your new scripts work fine for me. I did have to add the VALUE parameter to the setDrone$$anonymous$$ovementEnabled function before it would compile. See the update to my answer below.

2 Replies

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

Answer by Dave-Carlile · Dec 27, 2012 at 03:08 PM

Update: You're using isAlive like a function, but it's a variable. You need to call getAlive(). If you add the parentheses to isAlive you get the "cannot invoke" error because isAlive is a variable, not a function.

Original:

Don't you want to call getAlive() instead of isAlive?

  if(Camera.main.transform.parent.gameObject.GetComponent(Health).getAlive())
  {
  }

I'm assuming the Health component is the one attached to Object A.

The key thing you may be missing is the parentheses when calling the function. Without those it's trying to "invoke" the boolean expression, rather than call the boolean function.

Comment
Add comment · Show 3 · 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 Lovrenc · Dec 27, 2012 at 03:10 PM 0
Share

His pasted code doesent really say much, but if isAlive is public he can infact access it like this. However, you are right in that this is bad program$$anonymous$$g and he should use getter to access value.

avatar image Dave-Carlile · Dec 27, 2012 at 03:27 PM 0
Share

Yeah, there are a lot of assumptions here - the example and question could use some clarification.

avatar image Catlinman · Dec 27, 2012 at 04:16 PM 0
Share

I can't believe I actualy missed that... thanks for the help :)

avatar image
0

Answer by Golan2781 · Dec 27, 2012 at 04:37 PM

The error message "It is not possible to invoke an expression of type 'boolean'." means that somewhere in your code, you are trying to invoke (i.e. make a function call) on a boolean. This doesn't work as a boolean is obviously not a function and thus cannot be invoked.

It's the equivalent of calling

 true();

You might want to check where the error message names the problem; you probably have a double invocation due to a typo somewhere. Something like this:

 function returnBoolean() {
     return true;
 }
 
 function thisWorks() {
     if ( returnBoolean() )
         print('yes');
 }
 
 function thisWorksNot() {
     if ( returnBoolean()() )
         print('yes');
 }
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

11 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

Related Questions

Referencing booleans from settings 1 Answer

Using bool function with parameters 1 Answer

How do I call an enum from another script? 3 Answers

Bool not returning correct value from getCompoent 1 Answer

(C#) If statements based on variables in another script 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