Error With Blocks = Block1.Intersect(Block2); error CS0266:
On line 109 I get an error saying :
Assets/Code/GenGround.cs(109,34): error CS0266: Cannot implicitly convert type \`System.Collections.Generic.IEnumerable' to \`System.Collections.Generic.List<UnityEngine.GameObject>'. An explicit conversion exists (are you missing a cast?
But I am confused as they are the same type. How do I fix this? Thanks
using UnityEngine;
using System.Collections;
using System.Linq;
//using System;
using System.Collections.Generic;
public class GenGround : MonoBehaviour {
// Use this for initialization
public GameObject[] terrainTypes;
void Start () {
int x = 0;
int y = 0;
int i = 0;
GameObject[] itemList = new GameObject[10000];
terrainTypes = GameObject.FindGameObjectsWithTag ("Terrain").OrderBy( go => go.name).ToArray();
List<GameObject> WaterDeepOpts = new List<GameObject>();
WaterDeepOpts.Add(terrainTypes[3]);
WaterDeepOpts.Add(terrainTypes[4]);
List<GameObject> WaterShallowOpts = new List<GameObject>();
WaterShallowOpts.Add(terrainTypes[3]);
WaterShallowOpts.Add(terrainTypes[4]);
WaterShallowOpts.Add(terrainTypes[1]);
List<GameObject> SandOpts = new List<GameObject>();
SandOpts.Add(terrainTypes[0]);
SandOpts.Add(terrainTypes[1]);
SandOpts.Add(terrainTypes[4]);
List<GameObject> GrassOpts = new List<GameObject>();
GrassOpts.Add(terrainTypes[0]);
GrassOpts.Add(terrainTypes[1]);
GrassOpts.Add(terrainTypes[2]);
List<GameObject> StoneOpts = new List<GameObject>();
StoneOpts.Add(terrainTypes[0]);
StoneOpts.Add(terrainTypes[2]);
List<GameObject> Block1 = new List<GameObject>();
List<GameObject> Block2 = new List<GameObject>();
foreach(GameObject g in terrainTypes) {
Debug.Log (g);
};
itemList[i] = Instantiate (terrainTypes[Random.Range(0,terrainTypes.Length)], new Vector3(0,0,0),Quaternion.identity) as GameObject;
x = x + 10;
i = i + 1;
while (y < 1000) {
if (i > 0 && i < 100)
{
//Debug.Log (i);
if (itemList[i - 1].name == "WaterDeep(Clone)")
{
itemList[i] = Instantiate(WaterDeepOpts[Random.Range(0, WaterDeepOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
}
else if (itemList[i - 1].name == "WaterShallow(Clone)")
{
itemList[i] = Instantiate(WaterShallowOpts[Random.Range(0, WaterShallowOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
//Debug.Log (itemList [i]);
}
else if (itemList[i - 1].name == "Sand(Clone)")
{
itemList[i] = Instantiate(SandOpts[Random.Range(0, SandOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
}
else if (itemList[i - 1].name == "Grass(Clone)")
{
itemList[i] = Instantiate(GrassOpts[Random.Range(0, GrassOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
}
else if (itemList[i - 1].name == "Stone(Clone)"){
itemList[i] = Instantiate(StoneOpts[Random.Range(0, StoneOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
}
else {
itemList[i] = Instantiate(terrainTypes[Random.Range(0, terrainTypes.Length)], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
}
} else if (i > 100){
if (itemList[i - 1].name == "WaterDeep(Clone)")
{
Block1 = WaterDeepOpts;
}
else if (itemList[i - 1].name == "WaterShallow(Clone)")
{
Block1 = WaterShallowOpts;
}
else if (itemList[i - 1].name == "Sand(Clone)")
{
Block1 = SandOpts;
}
else if (itemList[i - 1].name == "Grass(Clone)")
{
Block1 = GrassOpts;
}
else{
Block1 = StoneOpts;
}
if (itemList[i - 100].name == "WaterDeep(Clone)")
{
Block2 = WaterDeepOpts;
}
else if (itemList[i - 100].name == "WaterShallow(Clone)")
{
Block2 = WaterShallowOpts;
}
else if (itemList[i - 100].name == "Sand(Clone)")
{
Block2 = SandOpts;
}
else if (itemList[i - 100].name == "Grass(Clone)")
{
Block2 = GrassOpts;
}
else{
Block2 = StoneOpts;
}
List<GameObject> Blocks = new List<GameObject>();
Blocks = Block1.Intersect(Block2);
} else {
itemList[i] = Instantiate (terrainTypes[Random.Range(0,terrainTypes.Length)], new Vector3(x,0,y),Quaternion.identity) as GameObject;
}
x = x + 10;
i = i + 1;
//Debug.Log(itemList[i]);
if (x == 1000) {
y = y + 10;
x = 0;
};
};
}
// Update is called once per frame
void Update () {
}
}
/*
WaterDeep {
WaterShallow
}
WaterShallow{
WaterDeep
Sand
}
*/
Answer by jgodfrey · Apr 19, 2016 at 10:17 PM
I think you need to convert the result back to a list. Specifically:
Blocks = Block1.Intersect(Block2).ToList();
Thanks I would have never have thought of that but it makes sense, I guess .Intersect makes an array.
Your answer
Follow this Question
Related Questions
What range of values need to be inserted if we need to pull a random item from the list? 0 Answers
Edit properties of certain list items,Changing properties of objects in a list (c#) 1 Answer
Using a specific function from a generic object. 0 Answers
How to delete an object if there is another object of the same type at that position ? 1 Answer
Getting objects out of a list and then comparing them 0 Answers