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 Nick4 · Jan 19, 2014 at 05:35 PM · booleanboolsendmessage

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!

Comment
Add comment
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

2 Replies

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

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))
Comment
Add comment · Show 4 · 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 Nick4 · Jan 19, 2014 at 06:11 PM 0
Share

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?)

avatar image fafase · Jan 19, 2014 at 06:12 PM 0
Share

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.

avatar image JoaoOliveira · Jan 19, 2014 at 06:15 PM 0
Share

Corrected my answer for using the generics version of GetComponent, that will provide the correct "Lab" type.

avatar image Nick4 · Jan 19, 2014 at 06:17 PM 0
Share

Thanks, now it works!

avatar image
2

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.

Comment
Add comment · Show 2 · 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 Nick4 · Jan 19, 2014 at 06:06 PM 0
Share

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.


avatar image Anxo · Jan 19, 2014 at 06:20 PM 0
Share

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

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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