Hello everyone, there is such a script:

public class Hero : MonoBehaviour { public CharacterController hero; public float speed = 10; public float gravity = 5F; private Vector3 moveDirection; GameObject[] tree = GameObject.FindGameObjectsWithTag("tree"); private AnimationClip NewAnimationn; // Use this for initialization void Start() { hero = gameObject.GetComponent<CharacterController>(); } // Update is called once per frame void Update() { Vector3 moveDirection = Vector3.zero; if (hero.isGrounded) { moveDirection = new Vector3(Input.GetAxis("Horizontal"), 0, Input.GetAxis("Vertical")); moveDirection = transform.TransformDirection(moveDirection); moveDirection *= speed; // раскоментировать для прыжка // if (Input.GetButton("Jump")) // moveDirection.y = jumpSpeed; } moveDirection.y -= gravity * Time.deltaTime; hero.Move(moveDirection * Time.deltaTime); if (Input.GetKey(KeyCode.A)) { for (int i=0;i<tree.Length;i++) { var p= tree[i].GetComponent<Animation>(); p.AddClip( NewAnimationn,"TimeLapse"); p.Play(); } } } 

There is a prefab tree on the stage, and not one, I added the tag "tree" to all prefabs - and on the line

 GameObject[] tree = GameObject.FindGameObjectsWithTag("tree"); 

- I search for objects by tag and stuff them into an array, then when I press the D button (I and the character move to the side) and I reboot this array - that is, I apply to each object with such a tag and its component animation and assign all this to a field named p, so that later on it call the AddClipp () method - with which to add to each prefab of the tree the animation I need, which I should pass as an argument to AddClipp (). How can I send a link to enter image description here then the general animation? Why ask? Because I do not know! After all, the project also created a file. enter image description here And as I understand it, the animation file itself, and the first is the so-called animator. Explain what's what. And the last - Unity exception enter image description here - when you press the A key. In the studio, this is the string for (int i=0;i<tree.Length;i++) - that is, the beginning of the cycle

  • one
    There is a unit Animation and there is an Animator - these are slightly different things. Therefore, at a minimum, you need to figure out what component there is in the tree and what method is hanging on it, depending on which you call ........ so what do you actually have there? - Alexey Shimansky
  • one
    By the way GameObject.FindGameObjectsWithTag("tree"); need to do in the Start or Awake method, because in the editor mode, he knows nothing about the objects of the scene .... so at the time of the start there will be nothing - Alexey Shimansky
  • "By the way GameObject.FindGameObjectsWithTag (" tree "); you need to do in the Start or Awake method" -Thanks. Corrected. Only one question - when I transfer GameObject.FindGameObjectsWithTag ("tree"); in Start - the field is perceived as local, to make it static does not allow it. How to be? - BadCatss
  • And yes, thanks for answering. - BadCatss
  • one
    You declare the variable itself in the class public GameObject[] tree; ...... and in the method already initialize void Start() { tree = GameObject.FindGameObjectsWithTag("tree"); } void Start() { tree = GameObject.FindGameObjectsWithTag("tree"); } - Alexey Shimansky

0