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?
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?)
private void Start()
{
doors = GameObject.FindGameObjectsWithTag("door");
doorBodies = new Rigidbody2D[doors.Length];
for(int i = 0; i < doors.Length;i++)
{
doorBodies _= doors*.GetComponent<Rigidbody2D>();*_
} }
void whenCalled() { for(int i = 0; i < doorBodies.Length;i++) { doorBodies*.transform.rotation = Quaternion.AngleAxis(-110, Vector3.forward);* } } Should give you what you need.
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*;*
// set rigidbody at index `i`*
_ rbs = door.GetComponent();_ * }* * }* * void whenCalled() {* * // loop through each Rigidbody2D and change its rotation.* * foreach (Rigidbody2D rb in rbs) {* * rb.transform.rotation = Quaternion.AngleAxis(-110, Vector3.forward);* * }* * }*