当我尝试在我的项目中获取精灵时,它总是= null,但我不明白为什么,我尝试GetComponent / GetComponentInChildren,它是一样的。< / p>
精灵是我的角色的孩子,就像这里:
这是代码:
new public Rigidbody2D rigidbody;
private Animator animator;
private SpriteRenderer sprite;
private void Amwake()
{
sprite = GetComponentInChildren<SpriteRenderer>();
rigidbody = GetComponent<Rigidbody2D>();
animator = GetComponent<Animator>();
}
private void Update()
{
if (Input.GetButton("Horizontal")) Run();
if (Input.GetButtonDown("Jump")) Jump();
}
private void Run()
{
Vector3 direction = transform.right * Input.GetAxis("Horizontal");
transform.position = Vector3.MoveTowards(transform.position, transform.position + direction, speed * Time.deltaTime);
sprite.flipX = direction.x < 0.0F;
}
答案 0 :(得分:-1)
你拼错了Awake
而写了Amwake
,所以Unity3D无法识别它并且它不会被调用。
只需重命名
private void Amwake()
到
private void Awake()