Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 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 /
avatar image
0
Question by Sericet1 · Nov 09, 2013 at 02:04 PM · error2d-tutorial

Class 2dAnimation

I am trying to make a 2D game. My approach, in terms of animation, is to simply change the materials as runtime. I figured a lot of my objects are going to need to animate so I made an ImageAnimation class which will take care of the initialization and the PlayAnimation.

I am new to Object oriented programming and I think I am getting hung up in my logic somewhere. I have attached below the two scripts,

I am receiving the error on the ImageAnimation script on line 32 "Object reference not set to an instance of an object.

Thank you very much for you help in advance.

ImageAnimation.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class ImageAnimation : MonoBehaviour 
 {
     
     public string fileName; //The filename of the animation sequence in the Resource folder.
     public bool loop; // Will make the animation repeat if true
     
     public float pictureRateInSeconds; //How long in between the frames
     public List<Texture2D> imagePictures; //Will hold all the images
      public int imageCounter; //Will be the index variable when going through the imags
      
     public float nextPic = 0f;  //  Holds the current time and will be checked to see if the next frame should begin
     
     void Awake()
     {
         imagePictures = new List<Texture2D>();     //Initilize the List 
         imageCounter = 1;
         pictureRateInSeconds = 0.04166666666666666666f;
     }
     
     
 
     public void InitializeImage(string fileName)
     {
         imageTextures = Resources.LoadAll( fileName, typeof(Texture2D)); //Loads all the images in the resource folder.
         for(var i = 0; i < imageTextures.Length; i++)
         {
              imagePictures.Add((Texture2D)imageTextures[i]); // Eror Line //Cycles through each image and adds them to the imagePictures
         }
         
         
         
     }
     public void PlayAnimation(bool loop)
     {
              
             
             bool toggle = true;
             if(Time.time > nextPic && toggle) 
             {
             
                            if(imageCounter >= imagePictures.Count)//if you reach the end of the animation
                         {
                             if(loop) //Reset the image counter
                             {    
                                 imageCounter = 1;
                             }
                             else //Toggle off the animation so it wont repeat
                             {    
                                 toggle = false;
                             }
                 
                         }    
                         
                         if(toggle) // When the animation is still true, change the material
                         {
                             nextPic = Time.time + pictureRateInSeconds;
                                renderer.material.mainTexture = imagePictures[imageCounter]; 
                             imageCounter += 1;
                         }
                        
                    
             }        
         
         
     }
 
 }


The Following is the script for: Player.cs

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class Player : MonoBehaviour
 {
     
     public ImageAnimation jump; // will hold the jump animation
     
     
     void Start()
     {
         jump = new ImageAnimation();  //jump variable is initlized to store all the jump images
         jump.InitializeImage("Jump"); //should begin the animation of "Jump"
     }
     
     void Update()
     {
 
     }
 }
 
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

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by Sericet1 · Nov 09, 2013 at 02:23 PM

I figure it out. I was trying to add monobehavior using "new". Changed it to addcomponent in the player script.

Fix

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 public class Player : MonoBehaviour
 {
     
     public ImageAnimation jump; // will hold the jump animation
     
     
     void Start()
     {
         jump = gameObject.AddComponent<ImageAnimation>();  //jump variable is initlized to store all the jump images
         jump.InitializeImage("Jump"); //should begin the animation of "Jump"
     }
     
     void Update()
     {
 
     }
 }
 
Comment
Add comment · 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
1

Answer by clunk47 · Nov 09, 2013 at 07:26 PM

 using UnityEngine;
 using System.Collections;
 
 public class Anim2D : MonoBehaviour 
 {
     Object[] frames;
     public enum Anim{Walk, Run};
     public Anim anim;
     float delay = .15f;
     int currentFrame = 0;
     
     void Awake()
     {
         if(anim == Anim.Walk)
         //FRAMES ARRAY WILL USE FOLDER: Assets/Resources/Textures/Character/Walk/... 
         //Any textures in this folder will be used in the array.
             frames = Resources.LoadAll("Textures/Character/Walk", typeof(Texture2D));
         
         if(frames.Length > 0)
             renderer.material.mainTexture = (Texture2D)frames[currentFrame];
         StartCoroutine(Animate());
     }
     
     IEnumerator Animate()
     {
         while(true)
         {
             yield return new WaitForSeconds(delay);
             if(currentFrame < frames.Length - 1)
                 currentFrame++;
             else
                 currentFrame = 0;
             renderer.material.mainTexture = (Texture2D)frames[currentFrame];
             yield return new WaitForEndOfFrame();
         }
     }
 }
 
Comment
Add comment · 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

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 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

Related Questions

Why do i keep getting this error? 0 Answers

Why can't I import all the files from character controller 1 Answer

c# parsing error Help please and the error is at where it says public string Name 1 Answer

Unity logs an error whenever I try to deallocate a nativearray, is there a proper way to do it? 1 Answer

Odd scripting error 2 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