- Home /
Accessing Functions
How can a custom function be used as a boolean inside an 'if' statement in a different script than the instance of the function?
Answer by sparkzbarca · Nov 17, 2012 at 06:21 PM
Ascript = script you want to make the call to B from
Bscript = script you want to call with the custom function in.
in the Bscript make the function public.
Aobject = gameobject Ascript is attached to
Bobject = gameobject Bscript is attached to
the easiest is to have both A and B script attached to the same gameobject
Otherwise you have to have Ascript search the scene for the object with B in it.
once you have Bobject.
Bobject.GetComponent<Bscript>().Function();
I already know that. I want the value of the functions returned by using an if statement:
if (GetComponent(aScript).setValue(true)) {
}
But, I get an error saying void can not be used in a boolean.
thats because your return type is void;
declare the function like this
public bool
NOT
public void
then in the function return true or false
public bool functionname(){
if (condition to return true) return true
else return false }
look up some c++ or C# function info to learn more on how functions work and how they can return information
It odd because I did that and it seems to disable the component rather than check if it's true or false.
lets see your code. That wont disable it. disabling is done via
gameobject.getcomponent<scriptname>().enable = false;
Your answer
