- Home /
animation.play plays on all cloned objects
Hi guys, here's the problem: I created a chest object trigger, which is the parent of the chest lid and main box and attached this script to it:
When I hix 'x' the animation plays the default clip that rotates the chest's lid
using UnityEngine;
using System.Collections;
public class ChestTrigger : MonoBehaviour {
// Use this for initialization
private Transform ChestTop;
private Transform Arrow;
private bool IsOpened = false;
public int ChestID = 0;
void Start () {
ChestTop = transform.Find("Stage1-Treasure-Chest-top");
Arrow = transform.Find("Stage1-Treasure-Chest-Arrow");
}
// Update is called once per frame
void Update () {
if (Input.GetKey("x") && !IsOpened){
ChestTop.animation.Play();
IsOpened = true;
Arrow.renderer.enabled =false;
}
}
void OnTriggerEnter(Collider collision)
{
if(!IsOpened){Arrow.renderer.enabled =true;}
}
void OnTriggerExit(Collider collision)
{
Arrow.renderer.enabled =false;
}
}
Now, when I clone the chest object and its childs ( ctrl-D ). when I hit 'x' the animation is played on all cloned objects as well. since i'm using transform.find to get to the clip, it should only affect the child right? and not all objects in the scene?
thanks for the help!
Answer by KiraSensei · Jul 15, 2013 at 01:13 PM
This script exists on all instances of chests, so the Update method is called for each chest. When you press "x", your "if" statement on every chest is true, so each chest is animated.
I suppose you have a character moving around ? What do you really want ? Do you want to open only the chest nearby the player when he hits "x" ? if so you need to check if the player is near the chest (there are several ways to do that, it depends on a lot of things ...)
Your answer

Follow this Question
Related Questions
Animation2D help! 1 Answer
Can the animation editor create local rotational data? 3 Answers
Adding animation clips via script 2 Answers
Can't Animate 0 Answers
Animation play on multiple object 1 Answer