Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Create a Path!

Discussion in 'Scripting' started by lugrana88, Jan 10, 2014.

  1. lugrana88

    lugrana88

    Joined:
    May 23, 2011
    Posts:
    68
    hi!

    This is my script but it's not work…

    the error is: Assets/-SCRIPTS/PathScript.js(17,45): BCE0019: 'position' is not a member of 'Object'.


    why?!?!?!

    Code (csharp):
    1.  
    2. var path : Array;
    3. var rayColor : Color = Color.black;
    4.  
    5. function OnDrawGizmos()
    6. {
    7.     Gizmos.color = rayColor;
    8.     var path_objs : Array = transform.GetComponentsInChildren(Transform);
    9.     path = new Array();
    10.    
    11.     for(var path_obj : Transform in path_objs)
    12.     {
    13.         if(path_obj != transform)
    14.             path[path.length] = path_obj;
    15.     }
    16.     for(var i : int = 0; i < path.length; i++)
    17.     {
    18.         var pos : Vector3 = path[i].position;
    19.         if(i>0)
    20.         {
    21.             var prev : Vector3 = path[i-1].position;
    22.             Gizmos.DrawLine(prev,pos);
    23.         }
    24.     }
    25. }
    26.  
     
  2. Bladesfist

    Bladesfist

    Joined:
    Jan 3, 2012
    Posts:
    107
    Should this not be?
    if(path_obj == transform)

    If I was you I would just use a generic collection.
     
  3. hpjohn

    hpjohn

    Joined:
    Aug 14, 2012
    Posts:
    2,190
    Yes absolutely stop using the bad, untyped js Array, GetComponentsInChildren already returns a builtin T[] array, so use that
    http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?

    Your first for loop only ever sets the last element in path, but you never add anything to path (does setting an element to an unsized Array automatically Push it? So bad)

    Anyway, if path is typed, you wont get the error about position not belonging to it
     
  4. lugrana88

    lugrana88

    Joined:
    May 23, 2011
    Posts:
    68
    Thankkkkkkksksksskskskkssksksksk!!!!!