- Home /
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!
btw Ignore the following // int score = 0; //string scoreText = "Score: 0";
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
I figured out, I had to place if (!animator) { animator = transform.GetComponentInChildren<Animator> (); }
in the Start Function lol. Thanks Anyway!!
Your answer
Follow this Question
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