Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

(UnityEngine.Transform)': not all code paths return a value

Discussion in 'Scripting' started by 1010, Feb 2, 2013.

  1. 1010

    1010

    Joined:
    Dec 1, 2012
    Posts:
    18
    Line(19) (UnityEngine.Transform)': not all code paths return a value
    Any help is always greatly appreciated.

    Code (csharp):
    1. public LayerMask mask = 1 << 8;
    2.    
    3.     public int Dis = 10;
    4.    
    5.     private int breakWhile = 1;
    6.    
    7.     public Collider[] closeWaypoints;
    8.    
    9.     public GameObject FindClosestWaypoint(Transform inTransform)
    10.     {
    11.         var inPosition = inTransform.position;
    12.         closeWaypoints = Physics.OverlapSphere(inPosition,Dis,mask);
    13.         while(closeWaypoints.Length < 3  breakWhile <10)
    14.         {
    15.             closeWaypoints = Physics.OverlapSphere(inPosition,Dis,mask);
    16.                
    17.             if(closeWaypoints.Length >0)
    18.             {
    19.                 return closeWaypoints[0].gameObject;   
    20.             }
    21.  
    22.            
    23.             Dis += 10;
    24.             breakWhile++;
    25.         }  
    26.     }
     
  2. alexzzzz

    alexzzzz

    Joined:
    Nov 20, 2010
    Posts:
    1,447
    What the function is supposed to return if closeWaypoints.Length equals to 3?
     
  3. fano_linux

    fano_linux

    Joined:
    Jan 1, 2012
    Posts:
    909
    create a GameObject before the 'while' execution and initialize it as null; then at the end of the code return the variable you have set.
    At the if(closeWaypoints.Lenght > 0 ) chage the return to variable_you_created = closeWaypoints[0].gameObject; and add a break; to stop the 'while';
     
  4. 1010

    1010

    Joined:
    Dec 1, 2012
    Posts:
    18
    getting WaypointOne' is assigned but its value is never used
    (19)UnityEngine.Transform)': not all code paths return a value
    Code (csharp):
    1. public LayerMask mask = 1 << 8;
    2.    
    3.     public int Dis = 10;
    4.    
    5.     private int breakWhile = 1;
    6.    
    7.     public Collider[] closeWaypoints;
    8.    
    9.     public GameObject FindClosestWaypoint(Transform inTransform)
    10.     {
    11.         var inPosition = inTransform.position;
    12.         closeWaypoints = Physics.OverlapSphere(inPosition,Dis,mask);
    13.         GameObject WaypointOne = null;
    14.         while(closeWaypoints.Length < 3  breakWhile <10)
    15.         {
    16.             closeWaypoints = Physics.OverlapSphere(inPosition,Dis,mask);
    17.                
    18.             if(closeWaypoints.Length >0)
    19.             {
    20.                 return WaypointOne = closeWaypoints[0].gameObject; 
    21.             }
    22.  
    23.            
    24.             Dis += 10;
    25.             breakWhile++;
    26.         }  
    27.     }
     
  5. Kaze_Senshi

    Kaze_Senshi

    Joined:
    Feb 19, 2012
    Posts:
    243
    If you create a function that returns something, it MUST return something always, look that the ONLY return in your function is inside of a if-clause, so if it is false, it will never return. You must guarantee that there is always one execution-path that goes until a return line, look this code at it's very end I put a return line, in any case you'll reach one of the following return lines.

    Code (csharp):
    1. public LayerMask mask = 1 << 8;
    2.  
    3.  
    4.    
    5.  
    6.  
    7.     public int Dis = 10;
    8.  
    9.  
    10.    
    11.  
    12.  
    13.     private int breakWhile = 1;
    14.  
    15.  
    16.    
    17.  
    18.  
    19.     public Collider[] closeWaypoints;
    20.  
    21.  
    22.    
    23.  
    24.  
    25.     public GameObject FindClosestWaypoint(Transform inTransform)
    26.  
    27.  
    28.     {
    29.  
    30.  
    31.         var inPosition = inTransform.position;
    32.  
    33.  
    34.         closeWaypoints = Physics.OverlapSphere(inPosition,Dis,mask);
    35.  
    36.  
    37.         GameObject WaypointOne = null;
    38.  
    39.  
    40.         while(closeWaypoints.Length < 3  breakWhile <10)
    41.  
    42.  
    43.         {
    44.  
    45.  
    46.             closeWaypoints = Physics.OverlapSphere(inPosition,Dis,mask);
    47.  
    48.  
    49.                
    50.  
    51.  
    52.             if(closeWaypoints.Length >0)
    53.  
    54.  
    55.             {
    56.  
    57.  
    58.                 return WaypointOne = closeWaypoints[0].gameObject;  
    59.  
    60.  
    61.             }
    62.  
    63.  
    64.  
    65.  
    66.  
    67.            
    68.  
    69.  
    70.             Dis += 10;
    71.  
    72.  
    73.             breakWhile++;
    74.  
    75.  
    76.         }  
    77.         return null; // null = empty, nothing to return
    78.  
    79.     }
     
  6. npsf3000

    npsf3000

    Joined:
    Sep 19, 2010
    Posts:
    3,830
    That's so much simpler than adding 'return null' to the end of the function...

    Or throw an Exception :p
     
  7. 1010

    1010

    Joined:
    Dec 1, 2012
    Posts:
    18
    Thanks for the help