Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 CodeNoise · Apr 07, 2020 at 06:29 AM · animationarraymodelcrashing

Model Animation with an Array

I'm working on a project where I have a series of models that I'm trying to animate using an array. There are 12 models total. The idea is that when I set each model on/off in the game environment as I iterate through the array, it will give the illusion of animation. The reason why I'm doing this is because I have no real experience with actual animation techniques, and this series of models is what I was provided with. I figured that this might be the best that I could do, but is this idea even feasible to begin with? Any insight on this would be greatly appreciated. I also have the code that I've written for the script I've been working on as well. Hopefully the logic for the AnimateModel method is sound, but if not please let me know. I hope that I've made both my problem and code clear enough. If not I would be happy to try and offer further clarity. Thanks.

EDIT: As I stated in a comment below, unfortunately something is not working correctly. I'm not getting the desired result. When I run the game all of the models seem to appear together at the same time. Once I stop the game all of the models are then turned off as well. There's no hint of the proper animation during runtime that I'm looking for. When I call the AnimateModel method in update it causes Unity to crash as well.

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ModelUpdate : MonoBehaviour
 {
     //Public and private objects/variables
     public GameObject[] models = new GameObject[12];
     public bool running = true;
 
     private bool firstPass = true;
     private int i = 0;
 
     //LoadArray iterates through the models array, instantiating GameObjects
     void LoadArray()
     {
         for (int i = 0; i < models.Length; i++)
         {
             models[i] = new GameObject();
         }
     }
 
     // Use this for initialization
     // Turning all of the models off to start with
     void Start()
     {
         for(int i = 0; i < models.Length; i++)
         {
             models[i].SetActive(false);
         }
     }
 
     /*
      * The primary method for iterating through the array. If during the first pass i == 0, don't bother with turning the previous model off.
      * If i == 0 and it's no longer the first pass, turn off model 11 and turn model 1 back on.
      * If i == 11, turn off the model that preceded it, turn model 11 on, set firstPass to false as it's no longer the first pass, and set i == 0 to restart the loop.
      * Otherwise iterate through the array, turning off the previous model first and turning the current model on to give the appearance of animation.
      * As long as the end of the array has not been reached, it is safe to increment i by one.
      */
     void AnimateModel()
     {
         while(running)
         {
             if (i == 0 && firstPass)
             {
                 models[i].SetActive(true);
             }
             else if(i == 0 && !firstPass)
             {
                 models[11].SetActive(false);
                 models[i].SetActive(true);
             }
             else if(i == 11)
             {
                 models[i - 1].SetActive(false);
                 models[i].SetActive(true);
                 firstPass = false;
                 i = 0;
             }
             else
             {
                 models[i - 1].SetActive(false);
                 models[i].SetActive(true);
             }
 
             if(i != 11)
             {
                 i++;
             }
         }
     }
     
     // Update is called once per frame
     //Originally AnimateModel() was called here, but this resulted in the program crashing with every run.
     void Update()
     {
         //AnimateModel();
     }
 }
Comment
Add comment · Show 3
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 zblase · Apr 07, 2020 at 06:40 AM 0
Share

Are you just looking for a more efficient way to achieve this, or does something about this script not work correctly?

avatar image CodeNoise zblase · Apr 07, 2020 at 06:54 AM 0
Share

Yes, unfortunately something is not working correctly. I'm not getting the desired result. When I run the game all of the models seem to appear together at the same time. Once I stop the game all of the models are then turned off as well. There's no hint of the proper animation during runtime that I'm looking for. When I call the Animate$$anonymous$$odel method in update it causes Unity to crash as well. Perhaps it's a problem with the Animate$$anonymous$$odel method, or I'm not doing something correctly with Update. I know that calling Animate$$anonymous$$odel by itself in Update may not be entirely wise, but I'm not sure how to get the model to constantly Update within the game without directly tying it to the frame rate.

avatar image zblase CodeNoise · Apr 07, 2020 at 07:17 AM 0
Share

I'm continuing this with an official answer so anyone else who has a similar problem doesn't have to dig through the comments to find a solution.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by zblase · Apr 07, 2020 at 07:16 AM

If you're activating every object in a while statement, then yes, all of your models will seem to appear at the same time. A while statement loops hundreds of times a second (depending on the complexity of your code inside said while statement). You need to implement something like a coroutine if you want your animations to start at different time intervals (https://docs.unity3d.com/Manual/Coroutines.html). Your models are turning off because you're initializing them in the code. Once the code stops, your models will disappear. The Update method gets called every time there is a new frame, so if you have complex code inside AnimateModel() then your program will crash because said method is repeatedly getting called. I'm not sure what you have in that method so I can't say for sure if that's the problem. Are you using an animation component on the GameObjects? I'm having trouble trying to understand what's going on with AnimateModel() and why you're calling it every frame.

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

325 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

Related Questions

Make mesh collider constantly change 1 Answer

Scale animation with model 1 Answer

Mesh suddenly started imploding while animating. 0 Answers

Can't find Mecanim? 2 Answers

Using animationState for animation controls 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