Answer the question
In order to leave comments, you need to log in
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();
}
}
}
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 for (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
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...
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...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]
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 questionAsk a Question
731 491 924 answers to any question