MissingComponentException:没有' SpriteRenderer'附属于" Koopa Troopa"游戏对象,但脚本正试图访问它

时间:2017-11-15 16:29:56

标签: unity3d unityscript

我正在复制超级马里奥游戏。目前收到如下错误:

  

MissingComponentException:没有' SpriteRenderer'附属于   " Gumbaa"游戏对象,但脚本正试图访问它。您   可能需要在游戏对象" Koopa中添加一个SpriteRenderer   Troopa&#34 ;.或者您的脚本需要检查组件是否已连接   在使用它之前。 UnityEngine.Renderer.get_bounds()(at   /Users/builduser/buildslave/unity/build/artifacts/generated/common/runtime/GraphicsBindings.gen.cs:1007)   EnemyMovement.Start()(在Assets / Scripts / EnemyMovement.cs:21)

每当我点击播放按钮时,我的游戏也会自动暂停。

Screenshot of inspector

这是完整的脚本:

 
using System.Collections;
using UnityEngine;

public class EnemyMovement : MonoBehaviour 
{
    public float speed = 2.7f;
    public LayerMask EnemyMask;

    Transform myTrans;
    float myWidth, myHeight;
    Rigidbody2D rb;
    SpriteRenderer mySprite;

    // Use this for initialization
    void Start () 
    {
        myTrans = this.transform;
        rb = this.gameObject.GetComponent<Rigidbody2D> ();

        mySprite = this.gameObject.GetComponent<SpriteRenderer> ();
        myWidth = mySprite.bounds.extents.x;
        myHeight = mySprite.bounds.extents.y;
    }

    // Update is called once per frame
    void FixedUpdate () 
    {
        Physics2D.IgnoreLayerCollision (8, 9);
        Vector2 LineCastPos = (myTrans.position.toVector2() +     myTrans.right.toVector2() * myWidth + Vector2.up * myHeight * 1.2f);

        Debug.DrawLine (LineCastPos, LineCastPos +      myTrans.right.toVector2 () * 1.2f);

        bool isBlocked = Physics2D.Linecast (LineCastPos, LineCastPos + myTrans.right.toVector2 () * 1.2f, EnemyMask);


        if (isBlocked) 
        {
            Vector2 currRot = myTrans.eulerAngles;
            currRot.y += 180;
            myTrans.eulerAngles = currRot;
        }

        Vector2 myVel = rb.velocity;
        myVel.x = myTrans.right.x * speed ;
        rb.velocity = myVel;
    }
}

0 个答案:

没有答案