Create a 2D Pie Charts in 3D space

Dear,
I need to create pie charts in 3D space from csv data. In the csv file are values that will be the coordinates in the space and also values that will be used for drawing the pie chart. I am completely new with Unity and it is very difficult for me. I found this thread and this code

It creates only 1 pie chart and so far I have managed to connect it with my csv data and also set the coordinates. I dont know how to create multiple pie charts. I tried with creating a List or an Array of objects but still doesnt work.

Here is what I have written in my Start function.

public List mPieChart;
float[ ] mData;
public TextAsset data;
public Vector3 Positions;
void Start()
{
//prepare data
string[ ] lines = data.text.Split(‘\n’);
for(int i=0; i<lines.Length; i++)
{
//split the lines
string[ ] attributes = lines*.Split(‘,’);*
float[ ] dataPositions = {
float.Parse(attributes[1], System.Globalization.CultureInfo.InvariantCulture),
float.Parse(attributes[2], System.Globalization.CultureInfo.InvariantCulture),
float.Parse(attributes[3], System.Globalization.CultureInfo.InvariantCulture)
};
Positions.x = dataPositions[0];
Positions.y = dataPositions[1];
Positions.z = dataPositions[2];
mPieChart = gameObject.AddComponent() as PieChartMesh;

mPieChart*.Init(dataPositions, 100, 0, 1, null, Positions);*
mPieChart*.Draw(dataPositions);*
}
}
It doesnt create anything. If I dont create a List but just one, then it creates only one of the pie charts.
Please guys, I am completely new and I am not sure what I am doing actually.
Thank you in advance.

I’m not familiar with that pie chart asset, but I doubt it’s intended to have multiple PieChartMesh components on the same game object.

Instead you should create separate objects for them, like this (after you get your Positions data):

GameObject obj = new GameObject();
var pieChart = obj.AddComponent<PieChartMesh>();
pieChart.Init(dataPositions, 100, 0, 1, null, Positions);  // (whatever all that stuff is!)
pieChart.Draw(dataPositions);

See how this creates a new GameObject for each chart, and then attaches a PieChartMesh to it. You don’t need your mPieChart field at all.

When you run this, if you look in your Hierarchy window, you should see a separate GameObject for each chart. You may have to adjust the code a bit to make sure they’re all drawn in the right positions, with the intended data.

Hey Joe,
Thanks for your respond. Unfortunately that didn’t work when I applied to my code.
There is only 1 game object created and is empty.
Here is a screen shot.

2950693--218763--Capture.PNG

The problem is in the pie chart code - it never adds a MeshFilter for some reason, which is required anytime you use a MeshRenderer. I would imagine that since it references a nonexistent component… checks screenshot again Yup, there’s a MissingComponentException in your console.

In the script from that zip file on that thread, add to the top of the script (just above “public class…”) [RequireComponent(typeof(MeshFilter))]. This should let Unity know it needs to add a MeshFilter to the object at the same time it adds that script.

I think @StarManta nailed it. But to be clear, you would do this in addition to the change I suggested above — you still need a separate GameObject for each chart (you can’t have more than one MeshFilter and one MeshRenderer on an object).

(The reason you only see one GameObject is because the MissingComponentException aborted the code after the first one.)

Hey guys, thank you for your help.
Even though, that did the trick, I still dont understand why now it is drawing only 1 part of the Pie.
When I draw only 1 Pie with the old script, then it is normal Pie chart with colors and 3 parts (as much as my data is).
When I draw more objects just like you told me, then I get pink colored 1 part of the pie…
As I said, I am very very new in Unity and don’t quite understand what is going on behind…