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 /
  • Help Room /
avatar image
0
Question by vespa39 · Dec 13, 2015 at 01:36 AM · mario

am getting this error in my script - `EnemyAI.CheckGround(UnityEngine.Vector3)': not all code paths return a value

here is my script the error is on line 78 Vector3 CheckGround (Vector3 pos) { I have tried several ways to correct this but still coming up with the same issue - how do I go about writing this code so I can fix this error or what do I need to change in the script so this can work properly - I tried using a debugger but of course it wouldn't tell me a solution on how to fix the issue. - if anyone can help me I would appreciate it

thanks in advance

public class EnemyAI : MonoBehaviour {

 public float gravity;
 public Vector2 velocity;
 public bool isWalkingLeft = true;

 LayerMask floorMask;

 private bool grounded = false;

 private enum EnemyState {

     walking,
     falling,
     dead
 }

 private EnemyState state = EnemyState.falling;

 // Use this for initialization
 void Start () {

     enabled = false;

     fall ();
 
 }
 
 // Update is called once per frame
 void Update () {

     UpdatedEnemyPosition ();
 
 }

 void UpdatedEnemyPosition () {

     if (state != EnemyState.dead) {

         Vector3 pos = transform.localPosition;
         Vector3 scale = transform.localScale;

         if (state == EnemyState.falling) {

             pos.y += velocity.y * Time.deltaTime;

             velocity.y -= gravity * Time.deltaTime;
         }

         if (state == EnemyState.walking) {

             if (isWalkingLeft) {

                 pos.x -= velocity.x * Time.deltaTime;

                 scale.x = -1;

             } else {

                 pos.x += velocity.x * Time.deltaTime;
                 
                 scale.x = 1;

             }
         }

         if (velocity.y <= 0)
             pos = CheckGround (pos);

         transform.localPosition = pos;
         transform.localScale = scale;
     }
 }

  Vector3 CheckGround (Vector3 pos) {

     Vector2 originLeft = new Vector2 (pos.x - 0.5f + 0.2f, pos.y - .05f);
     Vector2 originMiddle = new Vector2 (pos.x, pos.y - .5f);
     Vector2 originRight = new Vector2 (pos.x + 0.5f + 0.2f, pos.y - .05f);

     RaycastHit2D groundLeft = Physics2D.Raycast (originLeft, Vector2.down, velocity.y * Time.deltaTime, floorMask);
     RaycastHit2D groundMiddle = Physics2D.Raycast (originMiddle, Vector2.down, velocity.y * Time.deltaTime, floorMask);
     RaycastHit2D groundRight = Physics2D.Raycast (originRight, Vector2.down, velocity.y * Time.deltaTime, floorMask);

     if (groundLeft.collider != null || groundMiddle.collider != null || groundRight.collider != null) { 

         RaycastHit2D hitRay = groundLeft;
         
         if (groundLeft) {
             hitRay = groundLeft;
         } else if (groundMiddle) {
             hitRay = groundMiddle;
         } else if (groundRight) {
             hitRay = groundRight;

         }

         pos.y = hitRay.collider.bounds.center.y + hitRay.collider.bounds.size.y / 2 + .5f;

         grounded = true;
         
         velocity.y = 0;

         state = EnemyState.walking;

     } else {
         
         if (state != EnemyState.falling) {
             
             fall ();
         }
     }
 }


     void OnBecameVisible () {

     enabled = true;
 }

 void fall () {

     velocity.y = 0;

     state = EnemyState.falling;

     grounded = false;
 }

}

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
0

Answer by dhore · Dec 13, 2015 at 02:04 AM

Your function Vector3 CheckGround (Vector3 pos) is meant to return a Vector3, but you never return anything - that's the error.

I noticed that you changed the Y value of the parameter "pos" - are you trying to 'save' the return value straight into that parameter? Because if so the function should then be a void return and you need to use the out keyword on that parameter like so: void CheckGround (out Vector3 pos)

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

Answer by vespa39 · Dec 13, 2015 at 04:26 AM

I was able to fix my error - I just had a line a code in the wrong place - I appreciate you replying and helping me out - thanks again

happy holidays @dhore

Comment
Add comment · Show 3 · 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 dhore · Dec 13, 2015 at 04:51 AM 0
Share

Not a problem :) Have a good holiday season yourself too!

Also, you should probably mark the question as answered.

avatar image manogc · Jan 08, 2019 at 10:15 PM 0
Share

hi where was your error i am copying the same script and i did not find the error

avatar image Hellium manogc · Jan 08, 2019 at 10:49 PM 0
Share

The signature of the CheckGround function indicates it is supposed to return a Vector3, so either make this function return a Vector3 or replace Vector3 by void before CheckGround

 Vector3 CheckGround (Vector3 pos) {

 void CheckGround (Vector3 pos) {

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

35 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

Related Questions

i am having issues with my mario clone project 2 Answers

Mario moves highground? 0 Answers

super mario bros 3 sytle 3D platformer engine 0 Answers

Mario jump addforce problem 0 Answers

How to do an automatic scroll level in a 2D mobile game like in mario's game ? 2 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