How to loop to get all rigidbodies & rendercomponents?
Hello, I'm stuck in a project of mine. I would like to get all renders en rigidbodies from the objects stored in my array to be adressed at the same time when pressing space. If I do it now it will only work on 1 object in my array. I tried using a foreach loop, and i Hvave been playing around with creating new elements but it hasn't worked out; Can anyone help me out ?
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TestArray : MonoBehaviour { public GameObject[] myObjects; bool Switch; Rigidbody rigibod; public bool canSwitch; Renderer rend;
// Use this for initialization
void Start()
{
myObjects = GameObject.FindGameObjectsWithTag("Block");
for (int i = 0; i < myObjects.Length; i++)
{
rigibod=myObjects[i].GetComponent<Rigidbody>();
rend = myObjects[i].GetComponent<Renderer>();
}
Switch = true;
canSwitch = true;
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Space) && canSwitch == true)
{ Switch = !Switch; }
if (Switch == true)
{
rend.enabled = false;
Physics.IgnoreLayerCollision(10, 12, true);
rigibod.useGravity = false;
rigibod.velocity = new Vector3(0, 0, 0);
}
if (Switch == false)
{ //GetComponent<Renderer>().enabled = false;
rigibod.useGravity = true;
rend.enabled = true;
Physics.IgnoreLayerCollision(10, 12, false);
}
}
}
Answer by Cuttlas-U · Oct 26, 2017 at 06:08 AM
hi; the "rigibod" and "rend" are arrays too ? if so then u have to make an array according to myObjects lenght then change the i value as u are finding them so the code will be change this way :
void Start()
{
myObjects = GameObject.FindGameObjectsWithTag("Block");
rigibod = new Rigidbody[myObjects .Length];
rend = new Renderer[myObjects .Length];
for (int i = 0; i < myObjects.Length; i++)
{
rigibod[i] =myObjects[i].GetComponent<Rigidbody>();
rend[i] = myObjects[i].GetComponent<Renderer>();
}
$$anonymous$$an I love you :) I figured out that I had to make the rigibod en rend in an array too, this line is what I was missing. $$anonymous$$y code is fully functional right now. rigibod = new Rigidbody[myObjects .Length]; rend = new Renderer[myObjects .Length];
For anyone interested this is how it looks in the end : using System.Collections; using System.Collections.Generic; using UnityEngine;
public class TestArray : $$anonymous$$onoBehaviour { public GameObject[] myObjects; bool Switch; Rigidbody[] rigibods; public bool canSwitch; Renderer[] rend; public int i;
// Use this for initialization
void Start()
{
myObjects = GameObject.FindGameObjectsWithTag("Block");
rigibods = new Rigidbody[myObjects.Length];
rend = new Renderer[myObjects.Length];
for ( i = 0; i < myObjects.Length; i++)
{
Debug.Log(""+i) ;
rigibods[i]=myObjects[i].GetComponent<Rigidbody>();
rend[i] = myObjects[i].GetComponent<Renderer>();
}
/* foreach(GameObject c in myObjects)
{
rigibods[myObjects.Length] = c.GetComponent<Rigidbody>();
//rend= c.GetComponent<Renderer>();
}*/
Switch = true;
canSwitch = true;
}
// Update is called once per frame
void Update()
{
if (Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space) && canSwitch == true)
{ Switch = !Switch; }
if (Switch == true)
{
Physics.IgnoreLayerCollision(10, 12, true);
for (i = 0; i < rigibods.Length; i++)
{
rigibods[i].useGravity = false;
rigibods[i].velocity = new Vector3(0, 0, 0);
rend[i].enabled = false;
}
}
if (Switch == false)
{ //GetComponent<Renderer>().enabled = false;
for (i = 0; i < rigibods.Length; i++)
{
rigibods[i].useGravity = true;
rend[i].enabled = true;
}
// rend.enabled = true;
Physics.IgnoreLayerCollision(10, 12, false);
}
}
}
Your answer
Follow this Question
Related Questions
I cant reference Objects in an array. Int array works 2 Answers
save one big json string vs multi seperate json strings in playerprefs 0 Answers
How do you access the Scripts, Functions, and Bools within an array of GameObjects? 0 Answers
Please help! Audio not working in another class.. 0 Answers
Trying to change Source Image of UI Image via Script 2 Answers