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 Trace_Irving · Oct 29, 2012 at 12:15 PM · gameobjecttransformarray

How do I check multiple gameObjects transform positions?

I've been trying to make a shooting game where a load of boxes get knocked off of a pedestal, and a target comes down to shoot after wards. This is what I have so far.

 var cubeTag;
 var heightToCheck : float = 0.9f;
 var target:Rigidbody;
 var cubeArray = new Array ();
 cubeArray.length = 18;
 cubeArray.Push(GameObject.FindWithTag("cubeTag"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag1"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag2"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag3"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag4"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag5"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag6"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag7"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag8"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag9"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag10"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag11"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag12"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag13"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag14"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag15"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag16"));
 cubeArray.Push(GameObject.FindWithTag("cubeTag17"));
 
 for (var i = 0; i<cubeArray.length; i++) 
     { 
        var currentElementBeingTested = cubeArray[i]; 
        //do something with currentElementBeingTested 
        Debug.Log("cube array y position = "+currentElementBeingTested.Transform.transform.position.y);
     }
 
 function CheckForCubeHeight(){  //This function is to check to see if the cube has fallen off of the pedestal.
     var cubeToCheck = GameObject.FindWithTag("cubeTag2");
     //var cubeToCheck = cubeArray[0];
     //Debug.Log("reading cube as "+cubeToCheck);
     //Debug.Log("cubeToCheck  "+cubeToCheck.transform.position.y +"> Height is "+heightToCheck);
     if(cubeToCheck.transform.position.y < heightToCheck){
         Debug.Log("cube on floor. Reading y as "+cubeToCheck.transform.position.y);
         ///target.velocity = transform.TransformDirection (Vector3(0,1,0));
     }
     
 }
 
 function Update () {
     CheckForCubeHeight();
 }
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
0

Answer by Owen-Reynolds · Oct 29, 2012 at 01:55 PM

Not sure what the question is (do you want to check when all boxes are on the floor? What if some come to rest on other boxes?) But, you can simplify what you have, to make it easier to think about the rest.

Tags are often used to group several objects together. For example, you might have 50 cubes (walls... ) but only those 18 are the special falling boxes. So, you can make a single "fallBox" tag for all 18 of them. Imagine doing that from the start - you just make one, tag it, then copy and the correct tag is automatically on the new ones.

The purpose of the tag is whenever you want to say "cubes that count as falling boxes," you can use the tag. This can replace those 18 Add lines (and will work no matter how many boxes you have):

 cubeArray = GameObject.FindObjectsWithTag("fallBox"); // note the S in objects
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 Trace_Irving · Oct 30, 2012 at 02:59 PM 0
Share

Thanks, yeah I had a pretty strong feeling I was using tags wrong.

And sorry if the question wasn't very clear. What i want to do is check when all the boxes are on the floor (or more specifically, when they have fallen off of the pedestal they stand on). The boxes are pretty small compared to the height of the pedestal so I don't think them staking on top of each other will be an issue.

What I'm struggling with though, is a way to code this so that the program checks for when all of the cubes are on the floor.

avatar image
0

Answer by Trace_Irving · Oct 30, 2012 at 04:52 PM

Thanks, yeah I had a pretty strong feeling I was using tags wrong.

And sorry if the question wasn't very clear. What i want to do is check when all the boxes are on the floor (or more specifically, when they have fallen off of the pedestal they stand on). The boxes are pretty small compared to the height of the pedestal so I don't think them staking on top of each other will be an issue.

What I'm struggling with though, is a way to code this so that the program checks for when all of the cubes are on the floor.

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 Owen-Reynolds · Oct 30, 2012 at 10:56 PM

Arrays are a general topic. Unity arrays work pretty much the same as regular ones, and you can read about them lots of places. Putting together the parts you have:

 // count how many are too high. Done when none are too high
 var offFloor=0;
 for (var i = 0; i<cubeArray.length; i++) {
   if(cubeArray[i].transform.position.y > heightToCheck){
     offFloor +=1;
   }
 }
 if(offFloor==0) { they are all down } 

But this won't check they are at rest -- they could be bouncing back up.

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

11 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

Related Questions

C# Variables Transform vs GameObject 1 Answer

Getting the transform from a GameObject list 3 Answers

Keep adding targets to a list 2 Answers

Instantiating from an object that's in an array 1 Answer

How can i get ONLY the childrens of a GameOnbject with GetComponentsInChildren method? 5 Answers


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