Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by TheCount · Dec 26, 2013 at 07:58 PM · javascriptgameobjecttransformpositionracing

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

Comment
Add comment · Show 5
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Benproductions1 · Dec 27, 2013 at 02:13 AM 1
Share

Don't ever use Array. Use generic Lists ins$$anonymous$$d. You won't get this problem then :)

avatar image ArkaneX · Dec 27, 2013 at 09:24 AM 0
Share

Agree with Benproductions1 - use generic list. If you use Array class, then elements of this array are just objects (not Transforms) 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.

avatar image TheCount · Dec 27, 2013 at 04:43 PM 0
Share

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;
avatar image TheCount · Dec 27, 2013 at 04:50 PM 0
Share

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

avatar image Benproductions1 · Dec 27, 2013 at 11:00 PM 0
Share

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.

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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);
 }
Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image TheCount · Dec 27, 2013 at 05:50 PM 0
Share

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.

avatar image ArkaneX · Dec 27, 2013 at 08:18 PM 0
Share

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.

avatar image TheCount · Dec 27, 2013 at 08:27 PM 0
Share

Thank you so much it's working now!

avatar image
0

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

21 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Instantiate as a child at position 2 Answers

Position of a GameObject 2 Answers

Transform Checking on all Array Objects (JS) 3 Answers

Best way to detect if an object is near? 1 Answer

[CODE]Keep Gameobject position exactly at mouse cursor when cast float position to int 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges