2
\$\begingroup\$

I'm trying to set a SetTrigger parameter to call a different sprite animation on a campfire. I tried this also with a boolean, yet again the same error. parameter 'campfireNew' does not exist. Anyone familiar with this issue?

enter image description here

using UnityEngine;
using System.Collections;

public class WoodBehaviour : MonoBehaviour {

    bool campfire = false;
    private int count;
    public GUIText countText;   
    public int startingValue = 100; // The amount of value the fire starts the 
                                    // game with.
    public int currentValue;        // The current value the fire has.
    //public Slider valueSlider;    // Reference to the UI's health bar.
    bool inTrigger = false; 
    public int increaseValue = 25;  // The amount of health taken away per attack.

    Animator anim;                  // Reference to the Animator component.


    // Use this for initialization
    void Start () {

        count = 0;
        setCountText ();
        currentValue = startingValue;
        anim = GetComponent <Animator> ();
    }

    void Update () {

        if (inTrigger){

            if (Input.GetKeyDown(KeyCode.E) && count > 0){

                count -= 1;                                 // Minus one log
                currentValue += increaseValue;              // adds value
                anim.SetBool ("campfireNew", campfire);
                //  delay("200");
                setCountText ();                            // Updates HUD

                SendMessage ("PressedKey");

            }
        }
    }

    // OnTriggerEnter 2D
    void OnTriggerEnter2D(Collider2D other) {

        // If gameObject comes in contact with player
        if (other.gameObject.tag == "WoodStumps") {
            other.gameObject.SetActive (false);
            count += 1;
            setCountText ();
        }

        // If player comes in contact with gameObject
        if (other.gameObject.tag == "CampFire") {
            inTrigger = true;
        }


    }

    void OnTriggerExit2D(Collider2D other) {
        inTrigger = false;
    }

    void setCountText() {
        countText.text = "Count: " + count.ToString();
    }
}
\$\endgroup\$
2
\$\begingroup\$

maybe you don't get the reference to the right animator, to TEST (or DEBUG) define it public public Animator anim; , run the game and watch in the inspector if it has referenced the right one.

If the animator you need is attached to the same object where you attached the script it should work, but if they are not attached to the same game object it doesn't.

So if you want to access an animator of the campfire with the tag "CampFire" inside the playerscript you can do either replace: anim = GetComponent <Animator> (); with anim = GameObject.FindGameObjectWithTag("campfire").GetComponent<Animator>();;

Or also in the function

void OnTriggerEnter2D(Collider2D other) {

    // If gameObject comes in contact with player
    if (other.gameObject.tag == "WoodStumps") {
        other.gameObject.SetActive (false);
        count += 1;
        setCountText ();
    }

    // If player comes in contact with gameObject
    if (other.gameObject.tag == "CampFire") {
        //INSERT THE FOLLOWING LINE
        anim = other.GetComponent<Animator>();

        inTrigger = true;
    }


}
\$\endgroup\$
6
  • \$\begingroup\$ What you mentioned about creating "public Animator anim;" i tried this. The error still persists. However, you are right. this script is linked to my Player object. The SetTrigger animator is linked to the Campfire2 object. This might be the issue indeed. Now i need to find a way to link them to eachother. Do you have any suggestions or idea's or know if this is even possible? So basically in short. i want to call an Animator SetTrigger of a different gameobject inside the Player Script. \$\endgroup\$ Mar 27 '15 at 17:00
  • \$\begingroup\$ You can do it in many ways, if the campfire with the animator is the one that you tagged inside the 'OnTriggerEnter2D(Collider2D other)' you can assign the animator inside it, or simply inside the start() put : anim = GameObject.FindGameObjectWithTag("campfire").GetComponent<Animator>(); instead of anim = GetComponent <Animator> ();. I hope it work, i'm not sure let me know \$\endgroup\$
    – skind
    Mar 27 '15 at 18:23
  • \$\begingroup\$ Your code worked! now instead of using a Trigger parameter i want to use a Boolean. i made a Snipper and a tinypic of my current setup including notes. oi57.tinypic.com/n5ooz4.jpg snippi.com/s/7kpe2zo \$\endgroup\$ Mar 27 '15 at 19:22
  • \$\begingroup\$ I think that you can simply add a transition in the animator from Campfire2 to Campfire2 (to itself) with the same condition "campfireNew" as you did from Entry to Campfire2. It should work \$\endgroup\$
    – skind
    Mar 27 '15 at 19:36
  • \$\begingroup\$ I'm not entirely sure what you mean. Do you mean using a Boolean or still using a Trigger parameter? Also, in my Animator my Entry to Campfire2 transition has no condition. To clear things up, i want my Campfire to become a different sprite animation. The new animation is nested in my Campfire100 animation. I see no link to Campfire100 in your suggested solution. \$\endgroup\$ Mar 27 '15 at 20:03

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.