1
\$\begingroup\$

The player of my game can collect logs and can use them for hold on the fires. Each fire has its own status. So one is off and the other one is for 50% on.

I created a public int inside my script that is linked to my gameObject. Because it is public, I can change the value also in the Unity editor. See image:

enter image description here

The int is linked to a int parameter of "Intensity". And use this as follow:

public Animator anim;
public int campfireStatus;
public float timer;
public float beforeDecrease; // timer for campfires
bool inTrigger = false; 


// Use this for initialization
void Start () {

    anim = GetComponent<Animator>();

}

void OnTriggerEnter2D(Collider2D other) {
    if (other.gameObject.tag=="Player"){
        inTrigger = true;
    }
}

void OnTriggerExit2D(Collider2D other) {
    if (other.gameObject.tag=="Player"){
        inTrigger = false;
    }
}

// Update is called once per frame
void Update () {

    anim.SetInteger("Intensity", campfireStatus);
    //Debug.Log (campfireStatus);

    if(inTrigger && Input.GetKeyDown(KeyCode.E)) {
        if (WoodBehaviour.count > 0) {
            campfireStatus++;
        }
    }


    timer+= Time.deltaTime;

    if (timer > beforeDecrease) {

        timer = 0;

        if (campfireStatus !=0){
            campfireStatus--;
            Debug.Log ("Decrease");
        }

    }



}

But the added value inside the Unity editor will not be use by the script. It will start from the default state, as you can see here:

enter image description here

Each transition, like the one is selected, will have the condition: intensity is equals to 1 till 4.

So I look for a solution, so that I am be able to change which start animation should fired for that object, by changing the Campfire Status inside the Unity editor with the values of 1 till 4.

Look out for some advice!

\$\endgroup\$
  • \$\begingroup\$ try putting in 1. since it will start from defaul animation, a value of 2 won't trigger your animator as it requires to be 1 to go into the next state. if you want to skip 1 state you should put a transition from default to the desired state \$\endgroup\$ – Leggy7 Apr 3 '15 at 11:23
  • \$\begingroup\$ I don't know what's the exit condition from the second animation, but it looks like it exits, go back to idle and cannot trigger with 2. try putting >= 1 for first transition, >= 2 for transition from 2 to 3 and so on \$\endgroup\$ – Leggy7 Apr 3 '15 at 12:38
  • \$\begingroup\$ The conditional statement is equal to \$\endgroup\$ – Caspert Apr 3 '15 at 12:46
  • \$\begingroup\$ I have not unity here at work but can't you spec just >? in that case to have >= 2 just put >1 \$\endgroup\$ – Leggy7 Apr 3 '15 at 12:47
1
\$\begingroup\$

According to what evaluated in the comment section, your problem was that you had to set transition conditions to be greater then some_value instead of equal.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.