• 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
Question by dendens2 · Apr 18, 2013 at 05:33 AM · javascriptarrayclasses

Access a variable of a class stored in an array.

Ok, so I am still new to classes, but I am trying to use it in a pathfinding script. I've started off trying to use a script from a tutorial, but it was in C# and I really just needed to know the grid creation method. So now I have a class.

 class GridCell
 {
 var cellWalkable: boolean;
 var cellHeight: float;
 var cellX: float;
 var cellZ: float;
 var transformPosition: Vector3;
 
 
 
 }

This class is created multiple times to form the grid.

 for(var x = 0; x < mapWidth; x++)
     {
         for(var y = 0; y < mapHeight; y++)
         {
             //Gets the position for a ray
             var currentPosition = topLeftCorner + Vector3(x * cellSize, 0, y * cellSize);
             var hit: RaycastHit;
             //Creates a cell for the grid
             var cell = new GridCell();
             
             //Cast the ray
             if(Physics.Raycast(currentPosition, Vector3(0, -1, 0),hit, bounds.size.y))
             {
                 
                 if(hit.transform.gameObject.layer == walkableLayer)
                 {
                     cell.cellHeight = hit.point.y;
                     cell.cellX = hit.point.x;
                     cell.cellZ = hit.point.z;
                     cell.transformPosition = Vector3(cell.cellX, cell.cellHeight, cell.cellZ);
                     cellArray.Push(cell);    
                 }
             }
         }

At the end of that, I pushed all of the cells into an array called cellArray. In another script called ZombieScript, I am trying to get the transformPosition variable of the cell. I am currently trying to use this.

 function UpdateGrid()
 {
     var currentNode;
     var nearestDistance = Mathf.Infinity;
     
     for(var i = 0; i < navCubeScript.cellArray.length; i++)
     {
         if(Vector3.Distance(transform.position, navCubeScript.cellArray[i].transformPosition) < nearestDistance)
         {
         
         }
     }
     
     
 }

The problem I get, however, is that I get an error saying this.

 'transformPosition' is not a member of 'Object'.

Does anyone have any idea how to get a work around this? Thanks! :D

Comment

People who like this

0 Show 0
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

2 Replies

  • Sort: 
avatar image
Best Answer

Answer by Chronos-L · Apr 18, 2013 at 06:20 AM

You are using the Javascript array, which is "untyped", so the elements are stored as Object. So, when you access the element, you need to cast it to a GridCell to use transformPosition.

 ...
 for(var i = 0; i < navCubeScript.cellArray.length; i++)
 {
     if(Vector3.Distance(transform.position, ( navCubeScript.cellArray[i] as GridCell ).transformPosition) < nearestDistance)
     {
  
     }
  }
 ...



I would recommend you to switch to List < GridCell > as it is dynamic, and has better performance. If you need it to have mixed type, like what a Javascript array is, you can use List < Object >.


Reference:

http://wiki.unity3d.com/index.php?title=Which_Kind_Of_Array_Or_Collection_Should_I_Use?#Javascript_Arrays

Comment

People who like this

0 Show 1 · 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 dendens2 · Apr 18, 2013 at 02:07 PM 0
Share

Thank you!

avatar image

Answer by Eric5h5 · Apr 18, 2013 at 06:16 AM

Never use the JS Array class; use built-in arrays ideally (like GridCell[]), or a generic List (like List.< GridCell >) if it has to be a dynamically-sized array.

Comment
dendens2

People who like this

1 Show 0 · 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

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

13 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

Related Questions

Create an array of classes, of different type. 1 Answer

Arrays in Instantiate? 1 Answer

Creating a dynamic array of objects of custom class 1 Answer

Array of custom class objects all return the same value? 1 Answer

How to Create Dynamic Multidimensional Array? 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