• 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 theUndeadEmo · Oct 08, 2011 at 06:23 PM · arrays

Javascript gameobject array help

i want to be able to get all gameobjects with the name ("cube") from my scene and add it to an array.

not sure how

i've tried

var cube : GameObject[];

@Update cube.add(GameObject.Find("cube")); print(cube.Length);// to see if it worked

Comment
Add comment
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

3 Replies

· Add your reply
  • Sort: 
avatar image
-1
Best Answer

Answer by theUndeadEmo · Oct 08, 2011 at 08:19 PM

thanks for replies, found my answer here, but i am still stuck on how to make the enemy move onto the next closest waypoint (object) after it's been to one. (currently its just gets stuck to one) http://answers.unity3d.com/questions/16676/how-can-i-make-my-gameobject-find-the-nearest-obje.html

function Update () { var waypoints: GameObject[] = GameObject.FindGameObjectsWithTag("waypoint"); var closest: GameObject; var closestDist = Mathf.Infinity;

 for (waypoint in waypoints) { 
     var dist = (transform.position - waypoint.transform.position).sqrMagnitude; 

     if (dist < closestDist) { 
         closestDist = dist; 
         closest = waypoint; 
         } 
 } 
 transform.LookAt(closest.transform); 

}

Comment
Add comment · 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 aldonaletto · Oct 08, 2011 at 09:51 PM 0
Share

You should post this as a new question - nobody will find your question here, since it's classified as an answer!

avatar image
0

Answer by JepMaster · Oct 08, 2011 at 07:25 PM

I just finished working on something like this but I used TAGS to get all GameObjects of a certain type in an array. So in your case you would tag each game object you want under Cube, then do something like:

  var cubes : GameObject[]; // Define variable cubes as a built in array of GameObjects
 cubes = GameObject.FindGameObjectsWithTag("Cube"); // Find all game objects with the TAG "Cube". Note, you must actually create the tag and attach it to the game object for it to work.

Then you can do what you want with the results. For example, I wanted to get the names of each game object with the tag (in this case "Cubes") and push it into another array, so I did something like:

 var cubeNames = new Array(); // I used a new array because you can't push values into a builtInArray[];
 for (var value : GameObject in cubes) { //Loop though all game objects found in cubes...
     cubeNames.Push(value.name); // and push the "name" into the cubeNames array.
     //Debug.Log(value.name);  // Display results in the Debug.
 }

Hope this helped.

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
avatar image
0

Answer by G_Sacristan · Oct 08, 2011 at 07:17 PM

try adding manually via inspector

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Transform array help 1 Answer

How to set color to gameObject respective to below coding? 2 Answers

Ways to minimize Object.stelemref() calls (low level) 1 Answer

JesseEtzler's RPG Talent System in C# 1 Answer

Dungeon crawler problem(getting transform data from an 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