- Home /
Converting WWW to list for instatiation
I'm trying to make a list from a returned WWW to instantiate a number of items that can be clicked on. I'm getting an error Cannot implicitly convert type System.Collections.Generic.List<string>' to
System.Collections.Generic.List' and I'm not sure how to fix it.
Here are the scripts, please let me know what I need to do. Thanks!
VehicleIndex
public class VehicleIndex : MonoBehaviour
{
public string ID{ get; set;}
public string Make{ get; set;}
public string Model{ get; set;}
public string Year{ get; set;}
public string Mileage{ get; set;}
public VehicleIndex(string id, string make, string model, string year, string mileage)
{
this.ID = id;
this.Make = make;
this.Model = model;
this.Year = year;
this.Mileage = mileage;
}
}
Vehicle Index Script
public class VehicleIndexScript : MonoBehaviour
{
public GameObject ID;
public GameObject Make;
public GameObject Model;
public GameObject Year;
public GameObject Mileage;
public void DisplayVehicles(string id, string make, string model, string year, string mileage)
{
this.ID.GetComponent<Text>().text=id;
this.Make.GetComponent<Text>().text=make;
this.Model.GetComponent<Text>().text=model;
this.Year.GetComponent<Text>().text=year;
this.Mileage.GetComponent<Text>().text=mileage;
}
}
The Calling script
void Start ()
{
StartCoroutine ("GetAllVehicles");
}
public IEnumerator GetAllVehicles()
{
WWW VehicleData = new WWW ("http://localhost/CMVM/LoadVehicle.php");
yield return VehicleData;
string TheVehicleData = VehicleData.text;
string[] Vehicles = TheVehicleData.Split ('/');
VehicleList = Vehicles.ToList ();
Debug.Log (VehicleList.Count);
ShowAllVehicles ();
}
public void ShowAllVehicles()
{
Debug.Log("Show All Vehicles!");
for(int i = 0; i< VehicleList.Count; i++)
{
//Debug.Log (VehicleList [i]);
GameObject vObj = Instantiate(VehiclePrefab);
VehicleIndex tmpVehicle = VehicleList[i];
vObj.GetComponent<VehicleIndexScript> ().DisplayVehicles ("ID:" + tmpVehicle.ID, "Vehcile Make: " + tmpVehicle.Make, "Vehicle Model:" + tmpVehicle.Model, "Vehicle Year: " + tmpVehicle.Year, "Vehicle Mileage:" + tmpVehicle.Mileage);
vObj.transform.SetParent(DatabasedisplayParent);
}
}
Using LINQ, it does convert to a list and I confirmed that it is a list by debugging it out in a loop. This is my first time working with WWW.
Where and how is the VehicleList field defined/declared? It is probable that the type of this field is the problem since it may not be matching with the return type of Vehicles.ToList() (List). Also when you get an error like that in the console it tells you the exact line and script where the error occurred, if you could share that info it will be easier to help you.
Right here line 14 VehicleList = Vehicles.ToList (); Vehicle list is taken from VehicleIndex. Should it be string?
Yeah just make sure that wherever you declared the VehicleList field it is declared like this:
private List<string> VehicleList;
//I used private just for the example, that can be whatever modifier your code has right now
public List VehicleList = new List (); It was further up the script.
Not yet. The WWW returns a string, how then to make into the vechicleindex class in a list?
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
A node in a childnode? 1 Answer
New constructor help needed for JSON keyvalue pair 0 Answers
how to sort a list of gameobjects? 1 Answer