Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by zander353 · Oct 25, 2017 at 01:56 PM · unity 5arraygetcomponentloopinggetcomponents

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);
     }

 }

}

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

1 Reply

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

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>();
      }
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 zander353 · Oct 26, 2017 at 11:19 PM 0
Share

$$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

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

201 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


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