Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved Coroutine error

Discussion in 'Editor & General Support' started by NolandoFlorida, Apr 5, 2020.

  1. NolandoFlorida

    NolandoFlorida

    Joined:
    Nov 17, 2018
    Posts:
    6
    this is my first time posting here so bear with me. I'm trying to code a bounce pad of sorts, and it involves using a basic coroutine, but because of this error:

    'Exerter.Exert(Collider)': not all code paths return a value

    it's not working.

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Exerter : MonoBehaviour
    6. {
    7.     [Tooltip("Box collider that checks if the player is going to be bounced")]
    8.     public GameObject trigger;
    9.     bool bounces;
    10.     public bool singleUse;
    11.  
    12.  
    13.     [Tooltip("X force applied to the player")]
    14.     public float xForce;
    15.     [Tooltip("Z force applied to the player")]
    16.     public float zForce;
    17.     [Tooltip("Upwards force applied to the player")]
    18.     public float yForce;
    19.  
    20.  
    21.     void OnTriggerEnter(Collider other)
    22.     {
    23.         if (other.CompareTag("Player"))
    24.         {
    25.             StartCoroutine(Exert(other));
    26.         }
    27.     }
    28.  
    29.     IEnumerator Exert(Collider player)
    30.     {
    31.         PlayerMovement movement = player.GetComponent<PlayerMovement>();
    32.  
    33.         if (bounces)
    34.         {
    35.             if (movement.velocity.y <= 0)
    36.             {
    37.                 movement.velocity.x = xForce;
    38.                 movement.velocity.z = zForce;
    39.                 movement.velocity.y = yForce;
    40.  
    41.                 if(singleUse)
    42.                 {
    43.                     bounces = false;
    44.                 }
    45.             }
    46.         }
    47.     }
    48. }
    49.  
     
  2. Philip-Rowlands

    Philip-Rowlands

    Joined:
    May 13, 2013
    Posts:
    353
    You need to return something from this method. I usually use WaitForSeconds for that sort of thing.

    Code (CSharp):
    1. IEnumerator Exert(Collider player)
    2.     {
    3.         PlayerMovement movement = player.GetComponent<PlayerMovement>();
    4.         if (bounces)
    5.         {
    6.             if (movement.velocity.y <= 0)
    7.             {
    8.                 movement.velocity.x = xForce;
    9.                 movement.velocity.z = zForce;
    10.                 movement.velocity.y = yForce;
    11.                 if(singleUse)
    12.                 {
    13.                     bounces = false;
    14.                 }
    15.             }
    16.         }
    17.         yield return new WaitForSeconds(0.1f);
    18.     }
    To be honest, you could probably simplify this by replacing the Coroutine with Rigidbody.AddForce.
     
    NolandoFlorida likes this.
  3. NolandoFlorida

    NolandoFlorida

    Joined:
    Nov 17, 2018
    Posts:
    6
    I am not using rigid bodies, I programmed my own physics for movement.