【问题标题】:MissingComponentException: There is no 'SpriteRenderer' attached to the "Balletjes(Clone)" game object, but a script is trying to access itMissingComponentException:“Balletjes(Clone)”游戏对象没有附加“SpriteRenderer”,但脚本正在尝试访问它
【发布时间】:2019-01-09 22:13:33
【问题描述】:

我收到此错误:

“MissingComponentException:没有'SpriteRenderer'附加到“Balletjes(Clone)”游戏对象,但脚本正在尝试访问它。”

我想将精灵的名称分配给变量goldCoinPopUp。我搜索了很多,但没有任何效果。谁能帮帮我?

ItemDragHandler

public class ItemDragHandler : MonoBehaviour, IDragHandler, IEndDragHandler
{

    public GameObject prefab;

    Vector2 original;

    public void OnDrag(PointerEventData eventData)
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        Debug.DrawRay(ray.origin, ray.direction * 100);
        transform.position = Input.mousePosition;
    }

    public void OnEndDrag(PointerEventData eventData)
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        if (Physics.Raycast(ray.origin, ray.direction, out hit, 100)) 
        {
            if (hit.collider.gameObject.name == "Terrain") 
            {
                transform.gameObject.SetActive(false);
                GameObject obj = Instantiate(prefab,hit.point, Quaternion.EulerAngles(0f, 3f, 0f)); // 3d object
                obj.AddComponent<GenerateResources>(); // link to GenerateResources script
                Vector3 img = new Vector3(0, 8.66f, 0);
                obj.transform.position += img;
            } 
            else 
            {
                transform.localPosition = original;
            }
        }
        else 
        {
            transform.localPosition = original;
        }
    }

    void Start () 
    {
        original = transform.localPosition;
    }
}

生成资源

public class GenerateRessources : MonoBehaviour 
{
    private int ressourcesInBuilding;

    public int valueWhenCollect = 10;

    private float timerExtraRessource;

    public float generateRate = 4;

    public SpriteRenderer goldCoinPopUp;

    private GameObject currentGameObject;

    private GameObject Inventory;

    void Start () 
    {
        goldCoinPopUp = GetComponent<SpriteRenderer>();
        goldCoinPopUp.enabled = false;
        currentGameObject = gameObject.GetComponent<GameObject>();
        Inventory = GameObject.Find("Inventory");
    }

    void Update () 
    {
        AddResource();
        getResources();
    }

    private void AddResource()
    {
        timerExtraRessource += Time.deltaTime;
        if (timerExtraRessource >= generateRate)
        {
            ressourcesInBuilding++;
            timerExtraRessource = 0;
            if (ressourcesInBuilding >= valueWhenCollect)
            {
                goldCoinPopUp.enabled = true;
            }
        }
    }

    private void getResources() 
    {
        if (Input.touchCount > 0)
        {
            Touch touch = Input.touches[0];
            if (touch.phase == TouchPhase.Began)
            {
                RaycastHit hit;
                Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
                if (Physics.Raycast(ray, out hit, 100f))
                {
                    if (hit.transform.gameObject != null)
                    {
                    }
                }
            }
        }
        if (Input.GetMouseButtonDown(0))
        {
            RaycastHit hit;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (Physics.Raycast(ray, out hit, 100f))
            {
                if (hit.transform.gameObject != currentGameObject)
                {
                    if (goldCoinPopUp.enabled == true)
                    {
                        goldCoinPopUp.enabled = false;
                        Inventory.GetComponent<GameController>().Gold += ressourcesInBuilding;
                        ressourcesInBuilding = 0;
                    }
                }
            }
        }
    }
}

【问题讨论】:

    标签: unity3d sprite


    【解决方案1】:

    您的代码取决于存在的 SpriteRenderer,而您的预制件没有。您可以通过在类定义之前添加以下属性来解决此问题

    [RequireComponent(typeof(SpriteRenderer))]
    public class GenerateRessources : MonoBehaviour 
    

    但是,自动添加的 SpriteRenderer 不会附加任何精灵,因此更好的解决方案是确保您的预制件确实附加了合理的精灵

    【讨论】:

    • 我该怎么做?
    • 请注意,[RequireComponent] 标签只有在 GenerateRessources 新添加到 GameObject 时才有效。如果GenerateRessources 已经在 GameObject 上,添加标签之后会自动添加组件。来自文档:Note that RequireComponent only checks for missing dependencies during the moment the component is added to a GameObject. Existing instances of the component whose GameObject lacks the new dependencies will not have those dependencies automatically added.
    • @derHugo 请注意,根据发布的代码,有问题的组件确实是在运行时添加的
    • @Aricia 在编辑器中打开您的预制件(如果需要,将其添加到场景中),添加组件并保存预制件
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多