- Home /
Locating Position of Object not Working?
I have a set path that my AI needs to follow.
The path is an array like so:
function GetPath() {
var path_objs : Array = pathGroup.GetComponentsInChildren(Transform);
path = new Array();
for (var path_obj : Transform in path_objs){
if (path_obj != pathGroup)
path [path.length] = path_obj;
}
}
I try to find the z position of the next point by:
var nextPathPointPosition = path[currentPathObj].transform.position.z;
and that doesn't work. I get the error : "BCE0019: 'transform' is not a member of 'Object'."
please help
Don't ever use Array
. Use generic List
s ins$$anonymous$$d. You won't get this problem then :)
Agree with Benproductions1 - use generic list. If you use Array class, then elements of this array are just object
s (not Transform
s) and trying to access any Transform
related properties/methods requires casting.
If you have no idea what generic list is, then please read this wiki article.
Thanks but I still can't get my GetPath Function to work.
I read about the lists and changed the code to:
var path : List.<GameObject> = new List.<GameObject>();
var pathGroup : Transform;
function GetPath() {
var path_objs : List() = pathGroup.GetComponentsInChildren(Transform);
path = new List.<GameObject>();
for (var path_obj : Transform in path_objs){
if (path_obj != pathGroup)
path [path.count] = path_obj;
}
}
var nextPathPointPosition = path[currentPathObj].transform.position.z;
Just to state the goal of this code:
I have a gameObject that holds all the points in the path called "Path." I want to put all of it's children in a list in order. I want to be able to get the position of any of those children.
Thanks
Just so you know, Unity doesn't guarantee that the order in the heiarchy. Remember that there are certain things that will always work in the editor but are not guaranteed to work (and really shouldn't) in any build.
Answer by ArkaneX · Dec 27, 2013 at 05:12 PM
After reading your comment, I can suggest this approach:
var path = new List.<Transform>(pathGroup.GetComponentsInChildren.<Transform>());
path.Remove(pathGroup);
Now you have all child transforms in a list, and you can iterate through them accessing position.z:
for (var i : int = 0; i < path.Count; i++)
{
print(path[i].name + ": " + path[i].position.z);
}
Hmm I changed it to:
#pragma strict
import System.Collections.Generic;
var pathGroup : Transform;
var path;
var currentPathObj : int = 0;
function Start(){
path = new List.<Transform>(pathGroup.GetComponentsInChildren.<Transform>());
GetPath();
}
function GetPath() {
for (var i : int = 0; i < path.Count; i++)
{
print(path[i].name + ": " + path[i].position.z);
}
}
and I get the errors:
Object reference not set to an instance of an object Test.Start () (at Assets/Test.js:9
Assets/Test.js(14,32): BCE0019: 'Count' is not a member of 'Object'.
Assets/Test.js(16,11): BCE0048: Type 'Object' does not support slicing.
Assets/Test.js(16,33): BCE0048: Type 'Object' does not support slicing.
That's because in my code declaration and assignment of path variable was on a single line. In your code, path is declared in class body, without any type, so System.Object
is assumed. You have to change declaration to:
var path : List.<Transform>;
Additionally - I see you omitted path.Remove(pathGroup)
line. In this case, the list will contain transform object for the parent object as well, not only child transforms.
Answer by Tomer-Barkan · Dec 27, 2013 at 06:59 AM
You just need to cast:
var objTransform : Transform = path[currentPathObj];
var nextPathPointPosition = objTransform.position.z;