c# - 无法从 UnityEngine.Vector3 转换为 System.Collections.Generic.List<UnityEngine.Vector3>

标签 c# unity-game-engine

我正在制作一款全面 war 演示游戏,您可以在屏幕上拖动来移动您的单位。这个错误一直困扰着我,我不知道其背后的原因是什么。我得到的错误如下。

FormationScript.cs(119,44):错误CS1503:参数1:无法从“UnityEngine.Vector3”转换为“System.Collections.Generic.List

这是代码。 (对不起,它的长度)

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Debug = UnityEngine.Debug;

public class FormationScript : MonoBehaviour
{
    public GameObject[] units;
    public Transform formationBarrier;

    float unitWidth = 2.0f; // This also includes the gap between them, in this case the unit width is 1 and the gap is also 1

    private float numberOfUnits;
    private double unitsPerRow;
    private float numberOfRows;

    Vector3 startClick;
    Vector3 endClick;

    Vector3 selectedAreaSize;
    Vector3 selectedAreaPos;

    float startMinusEndX;
    float startMinusEndZ;

    private List<List<Vector3>> availablePositions;

    // Start is called before the first frame update
    void Start()
    {
    
    }

    // Update is called once per frame
    void Update()
    {
        Formation();
    }

    void Formation()
    {
        if (Input.GetMouseButtonDown(1))
        {
            Plane plane = new Plane(Vector3.up, 0);

            float distance;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (plane.Raycast(ray, out distance))
            {
                startClick = ray.GetPoint(distance);
            }
            Debug.Log(startClick);
        }
        if (Input.GetMouseButtonUp(1))
        {
            Plane plane = new Plane(Vector3.up, 0);

            float distance;
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            if (plane.Raycast(ray, out distance))
            {
                endClick = ray.GetPoint(distance);
            }
            Debug.Log(endClick);
        }
        // ====================================================================================================== 

        /*if (startClick.x - endClick.x < 0 || startClick.z - endClick.z < 0)
        {
            startMinusEndX = startClick.x + endClick.x;
            startMinusEndZ = startClick.z + endClick.z;
        }
        else
        {
            startMinusEndX = startClick.x - endClick.x;
            startMinusEndZ = startClick.z - endClick.z;
        }
        if (startMinusEndX > 0 && startMinusEndZ > 0)
        {
            formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
            formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);
        }
        else if (startMinusEndX < 0 && startMinusEndZ > 0)
        {
            formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
            formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) * 2, 0, (startClick.z + endClick.z) / 2);
        }
        */
        startMinusEndX = startClick.x - endClick.x;
        startMinusEndZ = startClick.z - endClick.z;

        formationBarrier.localScale = new Vector3(startMinusEndX, 1, startMinusEndZ);
        formationBarrier.localPosition = new Vector3((startClick.x + endClick.z) / 2, 0, (startClick.z + endClick.z) / 2);

        // ====================================================================================================== 

        selectedAreaSize = formationBarrier.localScale;
        selectedAreaPos = formationBarrier.localPosition;

        numberOfUnits = units.Length;
        unitsPerRow = (unitWidth / numberOfUnits) * selectedAreaSize.x;

        unitsPerRow = Math.Round(unitsPerRow, 0);

        if (unitsPerRow < 0)
        {
            unitsPerRow = unitsPerRow * -2;
        }

        if (numberOfRows % 1 == 0)
        {
            numberOfRows = numberOfUnits % (float)unitsPerRow;
            for (int i = 0; i > numberOfRows; i++) // i is the number of rows
            {
                //availablePositions.Add(i);
                for (int j = 0; j > numberOfUnits / unitsPerRow; j++) // j is the number of units / the units per row
                {
                    Vector3 pos = new Vector3((selectedAreaPos.x / ((float)unitsPerRow + 1)) + ((j - 1) * (selectedAreaPos.x / ((float)unitsPerRow + 1))), 0.0f, i * 2);
                    availablePositions.Add(pos); // Heres where the error's coming from
                }
            }
        }
        else if (numberOfUnits % (float)unitsPerRow != 0)
        {
            numberOfRows = numberOfUnits % (float)unitsPerRow;
        }

        Debug.Log(unitsPerRow);
        Debug.Log(numberOfRows);
    }
}

我对 Unity 还很陌生,所以放轻松:)

最佳答案

您的代码中存在语法错误。您正在将 Vector3 类型的 pos 插入到 availablePositions 中,这是 Vecotr3 列表的列表

更改 availablePositions 定义:

private List<Vector3> availablePositions;

或者在添加到 availablePositions 之前将 pos 转换为列表:

availablePositions.Add(new List<Vector3>{pos});

关于c# - 无法从 UnityEngine.Vector3 转换为 System.Collections.Generic.List<UnityEngine.Vector3>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62848034/

相关文章:

c# - 是否有必要在 Unity 中开发多种屏幕分辨率?

c# - Sonarqube v.4 TFS 任务 "Publish Analysis Result"抛出错误 "Could not fetch metrics"

c# - 如何防止 Asp.Net Core 从运行时存储引用依赖项

c# - 从文件中读取 double 值并将它们存储在数组中然后显示在列表框中

c# - WUApiLib IUpdateInstaller2 产生错误;一些操作系统更新安装其他人抛出 HResult -2145124318

android - Firebase 模块初始化失败 : remote_config (missing dependency)

c# - Unity 2D 中翻转 Sprite 的问题

c# - 如何从脚本在 Unity 中创建窗口并将其附加到现有选项卡?

animation - 如何在每次某个事件时使用动画师统一播放动画?

c# - Python 的 hash.digest() 使用什么 c# 等效编码?