• 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 acriticalstrike · Jun 12, 2013 at 03:12 AM · prefabsavetransform.position

How can I find the transform location of many game objects instantiated from a prefab? (game screen shots included!)

Hi! I've been playing around with the scripting in unity and I'm looking to just print the locations of game objects that are instantiated from a prefab in the game. The game is just a big open field where i can run around and spawn cubes by right clicking! I would like to (just in the debug log) print out the transform location of every cube the player has spawned.

Here's a fun image of the game: alt text

You see when I press save it does save the player location and then it loads the player at the location it saved... Now im going to do the same with the cubes instantiated in the scene, but of course first i need to print the transform locations of the cubes to the Debug.Log()

Any help would be greatly appreciated! THANKS!!! :-D

funwithcubes.png (328.0 kB)
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by robertbu · Jun 12, 2013 at 03:23 AM

You need to identify these game objects apart from other game objects. You could tag them all the same, name them all the same, or give them all the same component for example. Based on that attribute, you generate an array of the game objects and cycle through them. For example, if they (and only they) had the same tag, you could:

 #pragma strict  
 
 function Update() {
 
     if (Input.GetKeyDown(KeyCode.A)) {
         var gos = GameObject.FindGameObjectsWithTag("Enemy");
         for (var i = 0; i < gos.Length; i++)
             Debug.Log(gos[i].transform.position);
     }
 }

This will output to the console the position of all the game objects with the "Enemy" tag.

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 acriticalstrike · Jun 12, 2013 at 03:28 AM 0
Share

thank you, I will continue experimenting with the code! And yes, I've tagged the big face prefab as "cube"

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

14 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

Related Questions

Best method to save states in/for a prefab 0 Answers

How to save a prefab when a button is pressed 1 Answer

Save color prefab 0 Answers

How i can save persistent data for each scene? 1 Answer

How do I find a Prefab after I Replaced said Prefab? 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