[SOLVED] AnimationState.time doesn't work and returns NullReferenceException error

Hi guys.
I’m trying to code a simple weapon selector for my game: basically i’d like the main weapon to change frame when the user uses the scroll wheel of the mouse. I setup an animation with 2 keyframes called “WeaponAnimation” for my main weapon, and tried to use the AnimationState.time to change the frame of the animation: the fist keyframe is at 0s and the second one is at 1s, so i wrote:

 public Animation anim;
//other code here
anim["WeaponAnimation"].time = 1f;

But when I ran the game i got an error that was telling me that there was a problem with line 34, where i wrote the script to change the time of the animation… here’s the error:

here’s my full script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ProprietàArmi : MonoBehaviour
    private int Pistol = 0;
    private int AssaultRifle = 1;
    public static int Weapon;
    public static float TimeBetweenShots;
    public static float MagazineSize;
    public static float Damage;
    public GameObject MainWeapon;
    public Animator Animator;
    public Animation anim;

    public float[] Properties = { 0.1f, 7, 10 , 0.03f , 30 , 10}; // Time between shots, magazine size, damage

    private void Start()
        Weapon = Pistol; // set weapon on start
        UpdateWeapon(Weapon); // get properties of weapon
        //Animator = MainWeapon.GetComponent<Animator>();
        Animator.speed = 0f;
        //Animation = MainWeapon.GetComponent<Animation>();


    private void UpdateWeapon(int WeaponNew)
        Weapon = WeaponNew; // update weapon
        TimeBetweenShots = Properties[Weapon*3];
        MagazineSize = Properties[Weapon*3 + 1];
        Damage = Properties[Weapon*3 + 2];
        anim["WeaponAnimation"].time = 1f;

    void Update()
        if (Input.GetAxis("Mouse ScrollWheel") > 0f)
            if (Weapon == Properties.Length/3)
                Weapon = 0;
            } else
                Weapon += 1;
        } else if (Input.GetAxis("Mouse ScrollWheel") < 0f)
            if (Weapon == 0)
                Weapon = Properties.Length/3;
                Weapon -= 1;

Other informations:

  • the main weapon has both the animator
    and the animation component;
  • I left the animator unchanged except
    for the speed, which i set to 0;
  • Both the public animator and
    animation have their references in
    the inspector;
  • I’m using Unity 2020.1.0f1

I’m a complete beginner to Unity and C#, and this is my first time dealing with animations: thanks in advance for the answers, have a good day.

I actually solved the problem in another way, without animations:
I created an array of sprites where each element is a specific weapon, then i used the sprite renderer component of the main weapon to change it from the array.

public Sprite[] WeaponList; //insert the sprites in the editor

private void UpdateWeapon() {
// other code here...
 MainWeapon.GetComponent<SpriteRenderer>().sprite = Armi[Weapon];

Hope it can help people with my same issue :slight_smile:

This has nothing to do with Animations. The variable anim simply is not assigned or the requested value in anim["<value>"] does return null as the “value” does not exist.
Please read into how to solve Nullrefernce exceptions as they are not permitted here by the FAQ