- Home /
js to c# converting trouble
I'm converting a js script to a c# script but i am stuck on this part. I'm trying to draw a line between objects
for (int i=0; i<path.Count; i++) {
new Vector3 pos = path[i];
if (i>0) {
var prev=path[i-1].position;
new Gizmos.DrawLine(prev,pos);
Gizmos.DrawWireSphere(pos,0.6f);
}
}
edit :
prev=path[i-1].position is the big problem here i just cant get it to work.
this is the full script
var path:Array;
var rayColor:Color=Color.white;
function OnDrawGizmos(){
Gizmos.color=rayColor;
var path_objs:Array;
path_objs=transform.GetComponentsInChildren(Transform);
path=new Array();
for(var path_obj:Transform in path_objs)//dit kan een forEach zijn
{
if(path_obj!=transform)
{
path[path.length]=path_obj;
}
}
for(var i:int=0;i<path.length;i++)
{
var pos:Vector3=path[i].position;
if(i>0)
{
var prev=path[i-1].position;
Gizmos.DrawLine(prev,pos);
Gizmos.DrawWireSphere(pos,0.6);
}
}
}
edit : the answer was Transform pos = (Transform)path[i];
It seems you'd better stay in Js since you do not seem to know C#.
new Vector3 pos = path[i]; -> Vector3 pos = path[i];
new Gizmos.DrawLine(prev,pos); -> Gizmos.DrawLine(prev,pos);
that's great ... except it does not work because you cant do .position
this is the full script and yes it does work in js.
if you're the c# developer you say you are you can fix it can't you ? thanks in advance
var path:Array;
var rayColor:Color=Color.white;
function OnDrawGizmos(){
Gizmos.color=rayColor;
var path_objs:Array;
path_objs=transform.GetComponentsInChildren(Transform);
path=new Array();
for(var path_obj:Transform in path_objs)
{
if(path_obj!=transform)
{
path[path.length]=path_obj;
}
}
for(var i:int=0;i<path.length;i++)
{
var pos:Vector3=path[i].position;
if(i>0)
{
var prev=path[i-1].position;
Gizmos.DrawLine(prev,pos);
Gizmos.DrawWireSphere(pos,0.6);
}
}
}
Answer by stevethorne · Dec 18, 2013 at 03:04 PM
Update: this is using the full script you posted.
List<Transform> path;
Color rayColor = Color.white;
void OnDrawGizmos()
{
Gizmos.color = rayColor;
Transform[] path_objs;
path_objs = transform.GetComponentsInChildren<Transform>();
path = new List<Transform>();
foreach (Transform path_obj in path_objs)//dit kan een forEach zijn
{
if(path_obj != transform)
{
path.Add( path_obj );
}
}
for( int i = 0; i < path.Count; i++ )
{
Vector3 pos= path[i].position;
if( i > 0 )
{
Vector3 prev=path[i-1].position;
Gizmos.DrawLine( prev, pos );
Gizmos.DrawWireSphere( pos, 0.6f );
}
}
}
I tried as best as I could to interpret what you wanted from your js script and converted it to c#. I have no compile errors when I put this in my project.
I've also tested this code and it's working great in editor.
Your answer
![](https://koobas.hobune.stream/wayback/20220613124423im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
do you know what version of unity was released in the end of 2012? 0 Answers
Camera rotation around player while following. 6 Answers
Freeze rigidbody position without affecting rotation - please help! 1 Answer
making position checker - is area empty or not (with trigger)? 2 Answers
How to change animation in the code? 1 Answer