- Home /
[C#] how to add a primitive in this script???
Hi everyone, I working on a project trying to plot a nodes-edges representation but right now it just renders edges... how can I add a sphere primitive as node to get both gameObjects??
here is the script:
// nodes
foreach (Transform location in subregion)
{
try
{
int nodeindex = System.Convert.ToInt16(location.gameObject.name);
atlasRegionSubregionLocation currentNode = connectomeModel.NodesGlobalIndexer[nodeindex] as atlasRegionSubregionLocation;
int edgeinstanceIndex = 0;
foreach (int edgeIndex in currentNode.EdgeTo)
{
ConnectomeModel.Edge currentEdge = connectomeModel.Edges[edgeIndex];
// Look for gameobjects representing edges
Vector3 fromCoord = (GameObject.Find(currentEdge.NodeFrom.ToString())).transform.position;
//should be here??
GameObject sphere = (GameObject.CreatePrimitive(PrimitiveType.Sphere.currentEdge.NodeFrom.ToString())).transform.position;
// transform to local region
fromCoord = fromCoord - subregion.transform.position;
Vector3 toCoord = (GameObject.Find(currentEdge.NodeTo.ToString())).transform.position;
toCoord = toCoord - subregion.transform.position;
instanceEdge(fromCoord , toCoord , (float)currentEdge.Weight * 1.5f + 0.5f, edgeinstanceIndex, ref tempEdges, ref tempTriangles);
edgeinstanceIndex++;
}
[...]
thanks in advance
Answer by rutter · Jun 04, 2012 at 05:22 PM
Looks like you're created a new primitive, here:
GameObject sphere = (GameObject.CreatePrimitive(PrimitiveType.Sphere.currentEdge.NodeFrom.ToString())).transform.position;
Problem with that, though, is you don't seem to be setting the new GameObject's position. It might also be a good idea to parent it to some other object, to keep your scene hierarchy organized. Both of these can be achieved through the new object's transform.
You can see some simple examples here.
If this is an editor-only feature you're looking into, you might also consider using Gizmos to draw spheres, lines, and so on.
Answer by Wolfram · Jun 04, 2012 at 07:41 PM
GameObject sphere = (GameObject.CreatePrimitive(PrimitiveType.Sphere.currentEdge.NodeFrom.ToString())).transform.position;
Umh, this line doesn't make sense, this won't even compile. You're trying to dereference "PrimitiveType.Sphere", and casting the transform.position to a GameObject...
Try something like this:
GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
sphere.transform.position=fromCoord;
Answer by sebascontra · Jun 04, 2012 at 08:15 PM
hey! i just realized that, thanks! now I want to assign a certain size to the spheres and parent them to an empty object, but it doesn't work yet...
any idea?
Please don't add comments (and especially new questions) as answers. Add them as comments.
Just as you can set sphere.transform.position of your newly created spheres, you can set sphere.transform.localScale and sphere.transform.parent.