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