Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
2 captures
13 Jun 22 - 14 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
This question was closed Aug 28, 2020 at 12:25 PM by BigBuckBunny for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by BigBuckBunny · Aug 28, 2020 at 08:00 AM · animationframe

[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: alt text

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;
             }
             UpdateWeapon(Weapon);
         } else if (Input.GetAxis("Mouse ScrollWheel") < 0f)
         {
             if (Weapon == 0)
             {
                 Weapon = Properties.Length/3;
             }
             else
             {
                 Weapon -= 1;
             }
             UpdateWeapon(Weapon);
         }
     }
 }

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.

[EDIT] 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 :)

erroreunity.png (7.0 kB)
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

  • Sort: 
avatar image
0

Answer by Captain_Pineapple · Aug 28, 2020 at 08:26 AM

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

Comment
Add comment · Show 4 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image BigBuckBunny · Aug 28, 2020 at 10:11 AM 0
Share

Thank you! I actually found out that i got the error because i didn't check the property "Legacy" of the animation. However the animation still only shows the first frame. the main gun has both an animation and an animator component: alt text alt text

I don't know what could cause the issue...

animation.png (32.0 kB)
animator.png (82.7 kB)
avatar image Captain_Pineapple BigBuckBunny · Aug 28, 2020 at 12:19 PM 0
Share

Can you mark this question as answered and open a new one for this question? Because people who could actually help you here will not be able to find this question that you posted as a comment. I am not that good with the specifics of animators and animations.

avatar image BigBuckBunny Captain_Pineapple · Aug 28, 2020 at 12:22 PM 0
Share

Sure, no problem

Show more comments

Follow this Question

Answers Answers and Comments

330 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Mecanim Animation Event is Triggering Every Frame When it is Mirrored 1 Answer

create game and watch style frame by frame animation 0 Answers

Mecanim How get the last frame the animation? 0 Answers

Unity 2d Toolkit Specific Frame 1 Answer

Select especific frame with Animator 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges