我在Unity中的SpriteRender总是为空

时间:2018-05-16 03:02:37

标签: c# unity3d

当我尝试在我的项目中获取精灵时,它总是= null,但我不明白为什么,我尝试GetComponent / GetComponentInChildren,它是一样的。< / p>

精灵是我的角色的孩子,就像这里:

enter image description here

这是代码:

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;
}

1 个答案:

答案 0 :(得分:-1)

你拼错了Awake而写了Amwake,所以Unity3D无法识别它并且它不会被调用。

只需重命名

private void Amwake()

private void Awake()