Search Unity

Need help converting Java Array to C# Arrays

Discussion in 'Scripting' started by RoryScanlan, Aug 20, 2014.

  1. RoryScanlan

    RoryScanlan

    Joined:
    Jan 25, 2014
    Posts:
    139
    So basically im just trying to draw a line between some points, I have never used Array's and Im a little confused to weither c# even accepts them....

    I get this error at the moment
    Obviously im doing something wrong but i dont know what..

    Tutorials, Example Code, General Comments any help welcome :)

    Here is my code so far
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class AIPath : MonoBehaviour {
    6.  
    7.     public Transform[] paths;
    8.     public Color rayColor;
    9.  
    10.     void OnDrawGizmos()
    11.     {
    12.         Gizmos.color = rayColor;
    13.         Transform[] paths_objs = transform.GetComponentInChildren<Transform>();
    14.  
    15.         foreach (Transform path_obj in paths_objs)
    16.         {
    17.             if(path_obj != gameObject.transform);
    18.             {
    19.                 paths[paths.Length] = path_obj;
    20.             }
    21.         }
    22.  
    23.         for (int i = 0; i < paths.Length; i++)
    24.         {
    25.             Vector3 pos = new Vector3(paths[i-1].position.x, paths[i-1].position.y, paths[i-1].position.z);
    26.             if(i > 0)
    27.             {
    28.                 Vector3 prev = new Vector3(paths[i-1].position.x, paths[i-1].position.y, paths[i-1].position.z);
    29.                 Gizmos.DrawLine(prev,pos);
    30.             }
    31.    
    32.    
    33.         }
    34.     }
    35.  
    36. }
    37.  
     
    Last edited: Aug 20, 2014
  2. Fluzing

    Fluzing

    Joined:
    Apr 5, 2013
    Posts:
    815
  3. RoryScanlan

    RoryScanlan

    Joined:
    Jan 25, 2014
    Posts:
    139
    Thanks for the reply and the info. :)

    Thanks i got the arrays working but now its spamming the paths array, over and over.
    How can I run the loop just once?

    Code (CSharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using System.Collections.Generic;
    4.  
    5. public class AIPath : MonoBehaviour {
    6.  
    7.     public List<Transform> paths = new List<Transform>();
    8.     public Color rayColor;
    9.     public List<Transform> paths_objs = new List<Transform>();
    10.  
    11.     void Start()
    12.     {
    13.         foreach (Transform trans in transform.GetComponentInChildren<Transform>())
    14.         {
    15.             paths_objs.Add(trans);
    16.         }
    17.     }
    18.  
    19.     void OnDrawGizmos()
    20.     {
    21.         Gizmos.color = rayColor;
    22.  
    23.  
    24. // JUST NEED TO THIS RUN ONCE BUT IT SPAMS OVER AND OVER
    25.         foreach (Transform path_obj in paths_objs)
    26.         {
    27.             if(path_obj != this.gameObject.transform);
    28.             {
    29.                 paths.Add(path_obj);
    30.             }
    31.         }
    32.  
    33.         for (int i = 0; i < paths.Count; i++)
    34.         {
    35.             Vector3 pos = new Vector3(paths[i].position.x, paths[i].position.y, paths[i].position.x);
    36.             if(i > 0)
    37.             {
    38.                 Vector3 prev = new Vector3(paths[i-1].position.x, paths[i-1].position.y, paths[i-1].position.z);
    39.                 Gizmos.DrawLine(prev,pos);
    40.             }
    41.  
    42.  
    43.         }
    44.     }
    45.  
    46. }
    47.  
    *edit*
    Durp I just moved the code into start and it fixed it.
    *edit*
     
    Last edited: Aug 20, 2014