Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 Taureus2 · Jul 25, 2017 at 08:14 AM · updatefunctionbooleanreturn

Trying to create a function that returns a bool

Just trying to make a simple function that detects whether or not the user is touching the ground. Getting the error "Assets/Scripts/wallriding.cs(16,10): error CS0161: `wallriding.CheckIfMidAir()': not all code paths return a value". I know it's something to do with my if/else conditioning, but what do i need to change here?

void Update () { CheckIfMidAir(); }

 bool CheckIfMidAir()
 {
     if (Input.GetKeyDown(KeyCode.T))
     {
         Vector3 downward = transform.TransformDirection(Vector3.down) * 10;
         Debug.DrawRay(transform.position, downward, Color.green, 500f);

         print("bahh");

         Ray ray = new Ray(transform.position, transform.up * -1);
         RaycastHit hit;
         float distance = 18f;
         Physics.Raycast(ray, out hit, distance);
         if (hit.distance > 1f)
             {
                 print("offground");
                 return (true);
             }
         else
             {
                 print("onground");
                 return (false);
             }
         
     }
 }
Comment
Add comment · Show 1
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 screenname_taken · Jul 25, 2017 at 08:16 AM 0
Share

Shouldn't it be yield return? Or is that just for IEnumerators?

2 Replies

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

Answer by ShadyProductions · Jul 25, 2017 at 08:17 AM

Your method needs to return something, in the case that the T key was never pressed, you will never return anything, so you should add a default return. It's actually alot cleaner if you do it like this:

  private bool CheckIfMidAir()
  {
          Vector3 downward = transform.TransformDirection(Vector3.down) * 10;
          Debug.DrawRay(transform.position, downward, Color.green, 500f);
          print("bahh");
          Ray ray = new Ray(transform.position, transform.up * -1);
          RaycastHit hit;
          float distance = 18f;
          Physics.Raycast(ray, out hit, distance);
          if (hit.distance > 1f)
              {
                  print("offground");
                  return (true);
              }
          else
              {
                  print("onground");
                  return (false);
              }
  }

And then you call the method when you press the key.

      if (Input.GetKeyDown(KeyCode.T))
      {
               var inMidAir = CheckIfMidAir();
               if (inMidAir) {
                        //Do something
               }
      }
Comment
Add comment · Show 1 · 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 Taureus2 · Jul 25, 2017 at 12:31 PM 1
Share

I feel dumb, but i'm glad I asked. This helped me a lot, thanks :^)

avatar image
3

Answer by paul_hayes · Jul 25, 2017 at 08:31 AM

Can I just add that if you are returning a boolean, and you have a boolean evaluation in an if statement. You can shorten your code significantly by just returning that evaluation. As so:

 private bool CheckIfMidAir()
   {
           Vector3 downward = transform.TransformDirection(Vector3.down) * 10;
           Debug.DrawRay(transform.position, downward, Color.green, 500f);
           Ray ray = new Ray(transform.position, transform.up * -1);
           RaycastHit hit;
           float distance = 18f;
           Physics.Raycast(ray, out hit, distance);
           return hit.distance > 1f; // return the result of the evaluation, so returns true if hit.distance > 1f
   }
Comment
Add comment · Show 1 · 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 ShadyProductions · Jul 25, 2017 at 08:35 AM 0
Share

Good catch!

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

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

Related Questions

How can I use a yeild during an udate? 1 Answer

How to call a function only once in Update 1 Answer

Call A Function At A Certain Time 1 Answer

Boolean Trouble, Please help....Boolean does not flip another boolean 0 Answers

Get a public variable to the function. 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