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 /
This post has been wikified, any user with enough reputation can edit it.
avatar image
0
Question by JadeTheFlame · Aug 19, 2014 at 06:17 PM · gameobjectarrayvector3getcomponent

Obtaining an array of positions from an array of gameobjects

I need a list of positions from several gameObjects tagged as "Wall". I have searched all over, but only found specific answers to others' problems.

Would I use 'for()' or 'foreach()' here? In my LevelScript is:

 public Vector3[] GroundWallPosition;


And in my otherScript

 public GameObject[] GroundWallObjects;
 
     void Start ()
     {
         GroundWallObjects = GameObject.FindGameObjectsWithTag ("Wall");
         foreach (GameObject Wall in GroundWallObjects)
         {
             GetComponent<LevelScript>().GroundWallPosition[Wall] = GroundWallObjects[Wall].transform.position;
         }
     }

But obviously I cannot directly convert a gameObject into a Vector3, so how would I go about doing this?

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

2 Replies

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

Answer by jokim · Aug 19, 2014 at 06:36 PM

You're using a foreach loop as if it was an index based loop...

I don't believe you can use "Wall" as an index.

Try converting to a Index loop

     public GameObject[] GroundWallObjects;
     
     void Start ()
     {
         GroundWallObjects = GameObject.FindGameObjectsWithTag ("Wall");
         for (int i = 0; i < GroundWallObjects.Length; i++)
         {
             GetComponent<LevelScript>().GroundWallPosition[i] = GroundWallObjects[i].transform.position;
         }
     }
Comment
Add comment · Show 2 · 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 JadeTheFlame · Aug 19, 2014 at 07:51 PM 0
Share

Oh thank you very much, very clear and concise. However, I have run into a new problem, which is hopefully as easy to remedy. In another script I use the Vector3[] to compare my player position with the wall. $$anonymous$$y project is what you would consider a chess game to be, and I am comparing two positions, the before and after movements of the player. But if a player wants to move their piece off of the board, I cannot allow it, which is why I am trying to compare the positions of the player and the wall. I was sure this would work, but when I try to compare the two using:

 if ((transform.position + new Vector3 (0,-1,0)) != GetComponent<LevelScript>().GroundWallPosition)

It tells me that I cannot compare Vector3[] and Vector3. Is there an easier way to accomplish what I am trying to do?

avatar image jokim · Aug 19, 2014 at 09:42 PM 0
Share

Well, you'll need another loop here, to compare each and every one of the vector, if one match, then you turn a boolean to true.

Like so :

 bool wallHit = false;
 
 foreach (Vector3 wallPosition in GetComponent<LevelScript>().GroundWallPosition)
 {
     if (wallPosition == transform.position + new Vector3 (0, -1, 0))
     wallHit = true;
 }
 
 if (wallHit)
     //deny movement



avatar image
1

Answer by AngryBurritoCoder · Aug 19, 2014 at 06:55 PM

This is pretty simple, one way you can do this...

 public LevelScript script; //drag script into here
 
 public GameObject[] GroundWallObjects;
 
 void Start()
 {
    GroundWallObjects = GameObject.FindGameObjectsWithTag ("Wall");
 
    for(int = i; i < GroundWallObjects.Length; i++)
    {
       script.GroundWallPostion[i] = GroundWallObjects[i].transform.position;
    }
 }

 


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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

C# Array with random.range help? 2 Answers

Find GameObjects with a true boolean and put them in an array? 1 Answer

Find gameobject furthest from player along z axis 0 Answers

Using getcomponent with an array 2 Answers

Vector3 Position returns 0 when applied to GameObject 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