B
B
BadCats2016-12-05 21:23:30
C++ / C#
BadCats, 2016-12-05 21:23:30

Adding animation to prefab from another script?

Hello everyone, I have this 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 just one, I added the "tree" tag to all prefabs - and on the line
GameObject[] tree =  GameObject.FindGameObjectsWithTag("tree");
- I look for objects by tag and stuff them into an array, then when I press the D button (I have a character moving to the side) and I go through this array - that is, I refer to each object with such a tag and its animation component and assign all this to a field named p , so that later on it to call the AddClipp() method - with which to add the animation I need to each prefab of the tree, which I should theoretically pass as an argument to AddClipp() How can I pass a link to This
in the NewAnimationn field
724341399e914ba5bd033a18851f8192.JPG
animation at all? why am I asking? Because I do not know! After all, a file was also created in the project
6edd5f5a91324a2a9803780b5f66fc05.JPG
. And as I understand it, this is the animation file itself, and the first one is the so-called animator. Explain what's what.
And the last - Unity exception
c34a1e88f9624224b0546af638ea4965.JPG- when you press the A key. In the studio, this is the linefor (int i=0;i<tree.Length;i++)- i.e. the beginning of the cycle

Answer the question

In order to leave comments, you need to log in

4 answer(s)
D
Daniil Basmanov, 2016-12-06
@BadCats

Why are you adding animation at runtime? A prefab is a prefab because it can be configured in advance in the editor.
In the first picture you have an Animator Controller circled, it is inserted into the Animator component . The Animation component you are looking for in trees is the old way of animating objects, it is not animator compatible.
If you look closely at the documentation for GameObject.FindGameObjectsWithTag , you'll notice that this method is called at the start of the example. Moreover, in the console you should have written:
You used FindGameObjectsWithTag incorrectly, so your array of trees was uninitialized, which led to the exception.
Useful Links:
Official Tutorials
What is a Null Reference Exception?
Resolving Exception Issues: Sys...

T
TheTalion, 2016-12-05
@TheTalion

Why add tree animation from hero? Create a tree-on-tree animation.
In general, the animation must be assigned in the script itself. Declare the animation as public and drag the animation from your folder to the desired line in the script.
Why create an array of GameObject when you can use a sheet? Why iterate over an array with for and not foreach? What component do you select on the tree? Is he assigned there at all?
Lots of obvious mistakes. Learn the language. Start with this: https://professorweb.ru/my/csharp/charp_theory/lev...

S
Shamsudin Serderov, 2017-04-05
@Steein

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

index.php/code/ff/..

M
Maxim Timofeev, 2017-04-05
@webinar

Question in redirects or in the robot? If the question is robots, then
1. Make robot.txt and prohibit everything that is not necessary there
2. Find links to these urls on your project and fix it. After all, the robot did not accidentally hit them.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question