unity3d Unity:无法将类型“UnityEngine.Mesh”隐式转换为“UnityEngine.Mesh[]”

umuewwlo  于 2022-12-04  发布在  其他
关注(0)|答案(1)|浏览(286)

尝试在Unity中使用带网格的字符串时出现错误:
无法将类型“UnityEngine.Mesh”隐式转换为“UnityEngine.Mesh []”


错误代码:错误CS0029:无法将类型“UnityEngine.Mesh”隐式转换为“UnityEngine.Mesh []”

using System.Collections;
using System.Collections.Generic;
using UnityEngine.UI;
using UnityEngine;

public class HatSwitch : MonoBehaviour
{
    [SerializeField] private MeshFilter modelYouWantToChange;
    [SerializeField] private Mesh[] modelYouWantToUse;

    private int currentModel;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space))
        {
            modelYouWantToChange.mesh = modelYouWantToUse[currentModel];
            currentModel++;

            if (currentModel >= modelYouWantToUse.Length)
            {
                currentModel = 0;
            }
        }
    }

    public void ChangeMeshButton()
    {
        modelYouWantToChange.mesh = modelYouWantToUse[currentModel];
        currentModel++;

         if (currentModel >= modelYouWantToUse.Length)
         {
             currentModel = 0;
         }
    }

    public void ChangeMeshButtonBack()
    {
        modelYouWantToChange.mesh = modelYouWantToUse[currentModel];
        currentModel--;

        if (currentModel >= modelYouWantToUse.Length)
        {
            currentModel = 0;
        }
    }
}

尝试为游戏创建换肤器:
Seen here

ergxz8rk

ergxz8rk1#

我不知道是哪一行导致了异常,也不清楚modelYouWantToUse数组是如何填充的,而且我根本不了解Unity,但我会这样做:

public class HatSwitch : MonoBehaviour
{
    [SerializeField] private MeshFilter modelToChange;
    [SerializeField] private Mesh[] meshItems;

    private int currentMeshIndex = 0;

    // Update is called once per frame
    void Update()
    {
        if (Input.GetKeyDown(KeyCode.Space)) ChangeMesh();
    }

    public void ChangeMesh()
    {
        // Update the mesh
        modelToChange.mesh = meshItems[currentMeshIndex];

        // Increment our index
        currentMeshIndex++;

        // Validate index is in range
        if (currentMeshIndex >= meshItems.Length) currentMeshIndex = 0;
    }

    public void ChangeMeshBack()
    {
        // Update the mesh
        modelToChange.mesh = meshItems[currentMeshIndex];

        // Decrement our index
        currentMeshIndex--;

        // Validate index is in range
        if (currentMeshIndex < 0) currentMeshIndex = meshItems.Length - 1;
    }
}

相关问题