Search Unity

Question not all code paths return a value

Discussion in 'Scripting' started by JustYourFriendlyNeighbourhoodSlug, Jul 13, 2021.

  1. JustYourFriendlyNeighbourhoodSlug

    JustYourFriendlyNeighbourhoodSlug

    Joined:
    Aug 24, 2020
    Posts:
    24
    Working on a little project where the player has to get their character to an endpoint, I want to make it so that when the player touches the endpoint, it lerps to the centre of it. Because I want this game to have multiple characters all controlled by one player, there will end up being multiple endpoints, and because of this I will have to set the lerp's end coordinates by invoking a method with a coordinate parameter using the following code: https://pastebin.com/ysLwGLM6

    Unfortunately, however, when using this script, I get the following error:
    Assets\Scripts\PlayerMovement.cs(45,17): error CS0161: 'PlayerMovement.EndPointLerp(Vector3)': not all code paths return a value


    Any help greatly appreciated, along with any ways to generally simplify/optimise my existing code (I'm quite new to all this).

    Thank you.
     
    Last edited: Jul 13, 2021
  2. bobisgod234

    bobisgod234

    Joined:
    Nov 15, 2016
    Posts:
    1,042
    You need a "yield return null" in your coroutine EndPointLerp
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,992
    Your coroutine is missing a yield statement. You most likely want to add a
    yield return null;
    inside your while loop at the end.

    If you want to know more about coroutines, the IEnumerator interface or the yield keyword, have a look at my coroutine crash course.
     
  4. JustYourFriendlyNeighbourhoodSlug

    JustYourFriendlyNeighbourhoodSlug

    Joined:
    Aug 24, 2020
    Posts:
    24
    This is great thanks so much :)