- Home /
accessing an array of rigidbodies at once? c#
I'm currently looking to control multiple doors in a building all at once for simplicity instead of writing out a whole process per door. i have been looking at my code for a while now and can only seem to get it to pick up one door (so i know my tags are perfectly fine)
is their a way for me to access every rigid body in an array at once with this code?
 gameobject[] doors;
 rigidbody2d rb; 
 
 void start(){ 
         doors = GameObject.FindGameObjectsWithTag("door");
         rb = doors.GetComponent<Rigidbody2D>();    
 }
 
 void whenCalled(){    
   rb.transform.rotation = Quaternion.AngleAxis(-110, Vector3.forward);
   
 }
without the array i can rotate one door with this code but with the array i get this error message.
Error CS1061 'GameObject[]' does not contain a definition for 'GetComponent' and no extension method 'GetComponent' accepting a first argument of type 'GameObject[]' could be found (are you missing a using directive or an assembly reference?)
all help appreciated thank you in advance.
Answer by ElijahShadbolt · Feb 16, 2018 at 08:16 PM
GameObject[] is an array of GameObject instances. You can use GetComponent on each individual instance, but the array itself does not have a GetComponent method.
 GameObject[] doors;
 Rigidbody2D[] rbs;
 void Start() {
     doors = GameObject.FindGameObjectsWithTag("door");
     // initialize array with same number of elements.
     rbs = new Rigidbody2D[doors.Length];
     // loop through each GameObject and cache its rigidbody.
     for (int i = 0; i < doors.Length; ++i) {
         // get GameObject at index `i`
         GameObject door = doors[i];
         // set rigidbody at index `i`
         rbs[i] = door.GetComponent<Rigidbody2D>();
     }
 }
 void whenCalled() {
     // loop through each Rigidbody2D and change its rotation.
     foreach (Rigidbody2D rb in rbs) {
         rb.transform.rotation = Quaternion.AngleAxis(-110, Vector3.forward);
     }
 }
thanks for talking me through it, it makes much more sense now
Answer by Deathdefy · Feb 16, 2018 at 08:01 PM
     GameObject[] doors;
     Rigidbody2D[] doorBodies;
 
     private void Start()
     {
         doors = GameObject.FindGameObjectsWithTag("door");
 
         doorBodies = new Rigidbody2D[doors.Length];
         for(int i = 0; i < doors.Length;i++)
         {
             doorBodies[i] = doors[i].GetComponent<Rigidbody2D>();
         }
     }
 
     void whenCalled()
     {
         for(int i = 0; i < doorBodies.Length;i++)
         {
             doorBodies[i].transform.rotation = Quaternion.AngleAxis(-110, Vector3.forward);
         }
     }
Should give you what you need.
Your answer
 
 
             Follow this Question
Related Questions
Flip over an object (smooth transition) 3 Answers
Position and rotate 3 sprites perpendicular to the edges of a triangle 1 Answer
Why is this rotation acting odd? 0 Answers
Enemy Chase, transform, rotation Problem 0 Answers
Quaternions acting up 1 Answer
 koobas.hobune.stream
koobas.hobune.stream 
                       
                
                       
			     
			 
                