- Home /
Returning bool from another script (Cannot implicitly convert type 'void' to 'bool')
C# :
My main script :
void OnTriggerEnter(Collider other)
{
if(gameManager.GetComponent("Lab").SendMessage("Check",other))
{
// The things I'm going to do right after I solve this issue.
}
}
"Lab" script :
bool Check(Collider other)
{
if (other.transform.tag == "object_A")
return true;
else
return false;
}
But Unity gives an error saying " Cannot implicitly convert type 'void' to 'bool' ". Unity doesn't seem to let me return values between scripts. Maybe it's because SendMessage method does not receive values. How can I manage to return a value from a method that's in another script?
I don't want to use bools and send messages back because my main script will be attachted to at least 10 objects. I want to do it in a simple way. Thanks in advance!
Answer by JoaoOliveira · Jan 19, 2014 at 06:00 PM
Is there any reason for using SendMessage instead of calling the method directly? You just need Check to be public (as suggested by @Anxo replace this line:
if(gameManager.GetComponent("Lab").SendMessage("Check",other))
with this:
if(gameManager.GetComponent<Lab>().Check(other))
Then I get this error : Type UnityEngine.Component' does not contain a definition for
Check' and no extension method Check' of type
UnityEngine.Component' could be found (are you missing a using directive or an assembly reference?)
Indeed, Send$$anonymous$$essage returns void so you cannot expect a return value other than that. So your solution is to use GetComponent and make the method public. Send$$anonymous$$essage is more of a one way communication. It just tells the class to perform the method given but it does not matter about the result.
Corrected my answer for using the generics version of GetComponent, that will provide the correct "Lab" type.
Answer by Anxo · Jan 19, 2014 at 05:55 PM
C# default declaration is private. so it would have to be public bool but where are you returning that bool to? I would set it up like this in the "Lab" script.
public bool isChecked = false;
public void Check(Collider other)
{
if(other.transform.tag == "object_A")
isChecked = true;
else
isChecked = false;
}
Alternatively you could check if the tag == something before you send a call and save yourself the send.
That's not what I'm looking for. I have to return a bool directly in order to prevent complicated connections between game objects which would cost me lots of Send$$anonymous$$essage methods ins$$anonymous$$d of 1 per object. There must be a faster way to return values.
And I already tried declaring it public. It did not work. Thanks anyway.
well you are not returning it to anything. You cant say if(game$$anonymous$$anager.GetComponent("Lab").Send$$anonymous$$essage("Check",other)) b/c it will return a bool right?
If you just want to get the bool back to the first script you have to have a bool to assign it to.
void OnTriggerEnter(Collider other)
{
Lab myLab = game$$anonymous$$anager.GetComponent<Lab>();
if(myLab)
if(other.transform.tag == "object_A")
myLab.isChecked = true;
}
B/c right now you when you are Returning in your lab script you are sending a bool back to your void that is why you are getting the error
Your answer
Follow this Question
Related Questions
question about array or list of boolean 1 Answer
my button dosent work, became a light switch instead of a normal button (like a bell) 1 Answer
How to change function of a key with a boolean 1 Answer
I'm having trouble setting a bool. 2 Answers
Check bool from another script and use in another script C# 3 Answers