Search Unity

Question Assets/scripts/UziController.cs(53,17): error CS0161: 'UziController.ReloadAnim()': not all code pat

Discussion in 'Animation' started by efoue2, Sep 15, 2020.

  1. efoue2

    efoue2

    Joined:
    Jul 23, 2020
    Posts:
    14
    So, I have some code[down below] but it gives me 'not all code paths return a value' errors on lines 1 and 5 (for your purposes; this isn't the whole script!) and for the life of me, I can't figure out what I did wrong! Does anybody know what I did wrong? (I also am pretty sure this is legacy animation but idk)

    EDIT: All I had to do was make the void, but now the animations don't play!

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class UziController : MonoBehaviour
    6. {
    7.     public GameObject Player;
    8.     public Camera cam;
    9.     public GameObject Muzzle;
    10.     private Animator anim;
    11.    
    12.     [Header("Gun Properties")]
    13.     public GameObject Bullet;
    14.     public int maxAmmo;
    15.     public int burstSize;
    16.     public float bulletSpeed;
    17.     public int range;
    18.    
    19.     [Header("Idle Frame")]
    20.     public Sprite idleframe;
    21. //    [Header("Reload Frames")]
    22. //    public Sprite rf1;
    23. //    public Sprite rf2;
    24. //    public Sprite rf3;
    25. //    public Sprite rf4;
    26. //    public Sprite rf5;
    27.     public Sprite rf6;
    28.     public Sprite rf7;
    29.     public Sprite rf8;
    30.     public Sprite rf9;
    31.     public Sprite rf10;
    32.     public Sprite rf11;
    33.     [Header("Shoot Frames")]
    34.     public Sprite sf1;
    35.     public Sprite sf2;
    36.     public Sprite sf3;
    37.    
    38.     private static SpriteRenderer Renderer;
    39.     private Vector3 moveto;
    40.     private int ammo;
    41.    
    42.     void Fire(){
    43.         GameObject bulletInstance = Instantiate(Bullet, Muzzle.transform.position, Muzzle.transform.rotation);
    44.         Vector3 targetpos = Input.mousePosition.normalized*range;
    45.         float step =  bulletSpeed * Time.deltaTime; // calculate distance to move
    46.         bulletInstance.GetComponent<Rigidbody2D>().AddForce(Vector3.MoveTowards(Muzzle.transform.position, targetpos, step));
    47.         FireAnim();
    48.     }
    49.     void FireAnim(){
    50.         anim.SetTrigger("Shoot");
    51.         anim.ResetTrigger("Shoot");
    52.     }
    53.     void ReloadAnim(){
    54.         anim.SetTrigger("Reload");
    55.         anim.ResetTrigger("Reload");
    56.     }
    57.     void Start(){
    58.         Renderer=GetComponent("SpriteRenderer") as SpriteRenderer;
    59.         ammo=maxAmmo;
    60. //        Renderer.sprite=idleframe;
    61.         anim=GetComponent<Animator>();
    62.     }
    63.     // Update is called once per frame
    64.     void Update(){
    65.         //go to player
    66.         moveto=new Vector3(Player.transform.position.x,Player.transform.position.y,0);
    67.         transform.position=moveto;
    68.        
    69.         // point to mouse
    70.         Vector3 mousePos = cam.ScreenToWorldPoint(Input.mousePosition);
    71.         Vector3 perpendicular = Vector3.Cross(transform.position-mousePos,Vector3.forward);
    72.         transform.rotation = Quaternion.LookRotation(Vector3.forward, perpendicular);
    73.         //flip gun if on left side
    74.         if (Mathf.Abs(transform.rotation.z)>(float)0.7){
    75.             transform.Rotate(180.0f, 0.0f, 0.0f, Space.Self);
    76.         }
    77.        
    78.         //fire
    79.        
    80.         if (Input.GetMouseButton(0)){
    81.             if (ammo>1){
    82.                 ammo-=burstSize;
    83.                 Fire();
    84.                 Debug.Log("fired");
    85.                 Debug.Log(ammo);
    86.             }
    87.             else{
    88.                 Fire();
    89.                 ReloadAnim();
    90.                 ammo=maxAmmo;
    91.                 Debug.Log("reloaded");
    92.             }
    93.         }
    94.        
    95.     }
    96.  
    97. }
    Animation tree:
    Screen Shot 2020-09-15 at 8.00.00 AM.png
     
    Last edited: Sep 15, 2020
  2. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    You are calling SetTrigger then immediately cancelling it with ResetTrigger.

    Nothing you do to an Animator Controller will actually take effect until the next animation update.
     
  3. efoue2

    efoue2

    Joined:
    Jul 23, 2020
    Posts:
    14
    noice, but how would I wait until the animation is done to reset the trigger?
     
  4. Kybernetik

    Kybernetik

    Joined:
    Jan 3, 2013
    Posts:
    2,570
    A trigger will automatically reset when it gets used by a transition. That's the whole point of using Triggers rather than Bools.

    If you do need to reset it manually then you could use a coroutine to wait a frame first, but that starts making your code more convoluted.

    You might also be interested in Animancer (link in my signature). This is one of the main Problems with Animator Controllers which Animancer avoids by letting you play and control animations directly in scripts.
     
  5. efoue2

    efoue2

    Joined:
    Jul 23, 2020
    Posts:
    14
    ah okay thanks!