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

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.

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