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 Giova507 · May 27, 2014 at 11:04 PM · c#animatorhow-toinitialize

How to use the animator in children (C#)

Hello! My Game is a 2d Game.

I have a GameObject Player with the script characterManager that controls movement and Animations. The problem is that, I use different skins for the Player, 3 actually. Every Skin is a different sprite with different Animators but all the Animators Triggers are called in the same way.

 using UnityEngine;
 using System.Collections;
 
 public class CharacterManager : MonoBehaviour {
     
 
     
     Vector3 velocity = Vector3.zero;
     public Vector3 gravity;
     public Vector3 flapVelocity;
     public float maxSpeed = 5f;
     public bool shieldPowerUp = false;
 
 
     static CharacterManager myInstance;
 
 //    int score = 0;
 //string scoreText = "Score: 0";
 
     
     
     bool didFlap = false;
     float deathCooldown;
     
     Animator animator;
 
 
 
     
     public bool dead = false;
     public bool gameOver = false;
     public bool godMode = false;
     
     public static CharacterManager Instance
     {
         get
         {
             if (myInstance == null)
                 myInstance = FindObjectOfType(typeof(CharacterManager)) as CharacterManager;
             
             return myInstance;
         }
     }
     // Use this for initialization
     void Start () {
 
     
 
     }
     
     // Do Graphics & Input updates here
     void Update (){
         if (!animator) {
                         animator = transform.GetComponentInChildren<Animator> ();
                 }
 
         if (dead==false) {
                         
                     if (Input.GetKeyDown (KeyCode.Space) || Input.GetMouseButtonDown (0)) {
                         didFlap = true;
                         
                         }
                 }
         if(dead) {
             deathCooldown -= Time.deltaTime;
             
             if (deathCooldown <= 0) {
                 Time.timeScale = 0;
                 gameOver = true;
                 GameObject.Find("menu_button_pause").GetComponent<PauseControl>().paused = true;
             
             }
             
         }
         if (shieldPowerUp == true) {
 
                         godMode = true;
 
                 }
 
                 
         }    
         
     // Do physics engine updates here
     void FixedUpdate () {
         
         if (dead)
             return;
         
         
         velocity += gravity * Time.deltaTime;
 
 
         
         
         
         if (didFlap == true) {
 
                 animator.SetTrigger ("DoFlap");
 
         
             didFlap = false;
             if (velocity.y < 0)
                 velocity.y = 0;
             
             velocity += flapVelocity;
         }
         velocity = Vector3.ClampMagnitude (velocity, maxSpeed);
         
         transform.position += velocity * Time.deltaTime;
         
         
     }
     void OnCollisionEnter2D(Collision2D gameObject) {
         if(godMode)
             return;
 
         animator.SetTrigger("Death");
 
 
         dead = true;
         deathCooldown = 0.8f;
 
 
 
 
 }
 
     }



When I use the classic skin, everything is good. But when I change to another skin, the Animator animator doesn't initalize and the Other Skins Animations are'nt played and I think is because it just Initialized with the classic Skin and it can't do it again.

How do I make that my Animator Initialize with every Skin.

P.D. All the Skins are children of the Player GameObject P.D. When I Change Skin, the old skin's gameobject is disabled and the new one is enabled.

Thanks!

Comment
Add comment · Show 1
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 Giova507 · May 27, 2014 at 11:01 PM 0
Share

btw Ignore the following // int score = 0; //string scoreText = "Score: 0";

1 Reply

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

Answer by Java666 · May 28, 2014 at 09:14 PM

so your using different animator on different skins if they act the same why not just use the same one or you'd have to set it up so that the animator changes to the other one in code. so would have to add the 3 animators to the code and set it up so that only one is used based on the skin chosen. i know i have something set up to change pictures of a sprite thats 2d somewhere but not having luck finding it to give a sort of example hopefully just the idea will help ya out

Comment
Add comment · Show 1 · 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 Giova507 · Jun 09, 2014 at 09:20 PM 0
Share

I figured out, I had to place if (!animator) { animator = transform.GetComponentInChildren<Animator> (); } in the Start Function lol. Thanks Anyway!!

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

22 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

Related Questions

Multiple Cars not working 1 Answer

How to change sprite animator speed through text? [2D] 0 Answers

Distribute terrain in zones 3 Answers

Raycasting, LineRenderer & Layermasks 0 Answers

Editor Button Rotates Selected Object 1 Answer


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