user13715983
user13715983

Reputation:

cannot convert from UnityEngine.Vector3 to System.Collections.Generic.List<UnityEngine.Vector3>

I'm making a total war demo game where you drag on the screen to move your units. This bug has been bothering me and I don't know what's the reason behind it. The error I get is below.

FormationScript.cs(119,44): error CS1503: Argument 1: cannot convert from 'UnityEngine.Vector3' to 'System.Collections.Generic.List<UnityEngine.Vector3>'

Here is the code. (sorry for its length)

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

I am pretty new to Unity so go easy : )

Upvotes: 2

Views: 3528

Answers (1)

Maddie
Maddie

Reputation: 414

There is a syntax error in your code. You are inserting pos which is of type Vector3 into availablePositions, which is a List of List of Vecotr3.

Either change availablePositions definition:

private List<Vector3> availablePositions;

or convert pos to list before adding to availablePositions:

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

Upvotes: 2

Related Questions