- Home /
How to start an animation of the object itself by trigger enter in #C?
Hey folks, I am making a game where the main character can encounter bushes and branches. I intended to start an animation of these branches that makes them wave during an encounter with the player. I now have 2 scripts. one attached to the branch and one on the polygon collider which is on the branch. I am asking this question because I wasn't able to find this problem in #C
The branch scripts looks like this:
 using UnityEngine;
 using System.Collections;
 public class MovingBranch: MonoBehaviour {
     
     public bool PlayAnimation = false;
     
     
     void Start () {    
         
     }
     
     
     void Update () {
         if(PlayAnimation){
             // Whatever function or code you write to start the animation
             // or however you want to do the animation}
         }
     }
 }
 
And the script attached on the polygon collider looks like this:
 using UnityEngine;
 using System.Collections;
 
 public class Box: MonoBehaviour {
     // Make sure you have the Branch Object
     public GameObject Branch;
     // The Script of the Branch;
     private MovingBranch movingbranch;
     
     
     void Start () {    
         // This will get the script and set the trainscript
         movingbranch= Branch.GetComponent("MovingBranch") as MovingBranch ;
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     
     void OnTriggerEnter(Collider collision){
         if(collision.gameObject.tag == "Player"){
             movingbranch.PlayAnimation = true;
         }
     }
 }
I converted a existing script into my project but it simply doesn't work even though there are no errors. I don't know why the animation is not started when being touched by the main player? I am a beginner in scripting.
If somebody can point me into the right direction that would be really helpful :)
Answer by DreamEnder · Jun 15, 2015 at 10:32 AM
Put some Debug.Log messages in your methods and check if the player is colliding with the branch.
For now you could just replace:
 if(collision.gameObject.tag == "Player"){
        movingbranch.PlayAnimation = true;
 }
With:
 movingbranch.animation.Play();
To see if it works.
thanks for your quick reply, unfurtunately i got this;  
 

Ok try this:
 movingbranch.GetComponent<Animation>().Play();
Your answer
 
 
             Follow this Question
Related Questions
Getting My FPS Controller To Play An Animation After A Trigger Event 1 Answer
Trail of color behind the player 2D c# 1 Answer
Idle Animations, Player Walking 0 Answers
Mechanim trigger problem 1 Answer
Initiate Animation 0 Answers
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                