Search Unity

Problem executing Animation from Script

Discussion in 'Animation' started by GiovanniHDZ, Mar 6, 2019.

  1. GiovanniHDZ

    GiovanniHDZ

    Joined:
    Jan 31, 2019
    Posts:
    57
    I have an animation that I want to be played only on a certain number of the streak.
    The streak counter goes up and does its job well.
    This is the code I use to play the animation.

    public class Move2XSprite : MonoBehaviour {

    Animation Move2X;

    public void Start () {
    Move2X = this.gameObject.GetComponent<Animation> ();
    // ANIMA2X(); if I delete the diagonals the animation executes in the first frame of the game.
    }
    public void Anima2X() {
    Move2X.Play ("Moving2X");
    }
    }

    To bring the function Anima2X to another script I did it this way.


    GameObject M2XS;
    ....
    ....
    M2XS = GameObject.Find ("Move2XSprite"); (this its on void Start)
    ...
    ...
    void Sprite2X(){
    M2XS.GetComponent<Move2XSprite>().Anima2X(); (I make the function for execute the Anima2X )
    multiplier=2;
    }
    .....

    I tried to execute de function and I can't do it
    If I try execute from here....
    public void AddStreak(){
    streak++;
    if (streak >= 80)
    multiplier = 8;
    else if (streak >= 40)
    multiplier = 4;
    else if (streak >= 30)
    multiplier = 3;
    else if (streak >= 20)
    multiplier = 2;
    else if (streak = 20)
    Sprite2X ();
    else
    multiplier = 1;
    UpdateGUI ();
    }
    The error its "CS0029; cannot implicitly convert type int to bool"
    And if I try to execute Sprite2X (); from void start this its the error

    NullReferenceExeption: Object reference not sweet to an instance of an object.
    NameOfTheScriptWhereITryToExecute.Sprite2X()

    Help?
    I'm very newbie, and I do not know what to do anymore

    The sprite its on legacy...
    I tried to make Anima2X a Public static void...
    doesn't works