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 sheffieldlad · Nov 09, 2013 at 10:31 PM · rigidbodylistunity 4.2

[SOLVED]Adding and manipulating rigid bodies for more than one object via a script.

Hello,

I have set of blocks on the screen, bat and a ball, arkanoid style.

When the player loses all his lives I want to add a rigidbody component to the remainder of the blocks and have them tumble down the screen using Unities gravity.

The blocks are placed into position in the Unity editor and told which 'type' to be with the script.

they are then added to a list like so:

 public List<GameObject> Blocks;
 
 void Awake() {
         
         Blocks=new List<GameObject>();
 }

This in my Start()

 Blocks.AddRange(GameObject.FindGameObjectsWithTag("Block"));


this works fine, I can debug out to the console the Blocks.Count and see how many blocks are left at runtime.

When a block is destroyed it is removed from the list.

When the player loses all his or her lives I want to apply a rigidbody component to each remaining block in the list and add force to it so it falls down the screen.

I have tried the following....

 for(int cnt = 0; cnt < Blocks.Count; cnt++)  //while cnt is smaller than the length of the list do the following....
             
         {
             Debug.Log ("Did we make it into the function?");     //debug mssg to the console to check we are in the function
         
         Rigidbody mybody = Blocks.AddComponent<Rigidbody>();    //add a rigid body
         mybody.mass =1;                                            //add mass to the rigid body
         mybody.useGravity = true;                                //add gravity to the rigid body
         }


which gives me the following error....

Assets/Scripts/BallScript.cs(88,43): error CS1061: Type System.Collections.Generic.List' does not contain a definition for AddComponent' and no extension method AddComponent' of type System.Collections.Generic.List' could be found (are you missing a using directive or an assembly reference?)

I sort of understand why because I think I am referencing the list itself rather than the objects in the list.

How do I reference the objects rather than the list itself?

My blocks are all tagged with the name "Blocks"

I have tried doing GameObject.FindObjectsWithTag("Blocks") which throws me an error saying...

Assets/Scripts/BallScript.cs(82,29): error CS0029: Cannot implicitly convert type UnityEngine.GameObject[]' to UnityEngine.GameObject'

If I should be using the singular FindGameObjectWithTag instead of the plural when searching for more than one object with a tag then what is the plural version for? Also, why does it work when I use FindObjectsWithTag to add the blocks to the list? In any case, it didn't work.

Any help on how to iterate over my list of game objects and add a rigid body to each one would be greatly appreciated.

Many thanks,

Paul.

Comment
Add comment · Show 1
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 sheffieldlad · Nov 09, 2013 at 09:18 PM 0
Share

DOH!

Like most things, when you sit down and write the problem out you figure it out quite quickly.

my new code for going through the list looks like this.

 oreach(GameObject go in Blocks)
         {
             Debug.Log ("Did we make it into the function?");     //debug mssg to the console to check we are in the function
         
         Rigidbody mybody = go.AddComponent<Rigidbody>();    //add a rigid body
         mybody.mass =1;                                            //add mass to the rigid body
         mybody.useGravity = true;                                //add gravity to the rigid body
         }


and it seems to work fine.

I don't see a button to answer my own question so I've put my solution in a comment.

Apologies if this is not the accepted thing to do.

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer Wiki

Answer by sheffieldlad · Nov 10, 2013 at 12:13 AM

DOH!

Like most things, when you sit down and write the problem out you figure it out quite quickly.

my new code for going through the list looks like this.

  foreach(GameObject go in Blocks)
     {
     Debug.Log ("Did we make it into the function?"); //debug mssg to the console to check we are in the function
      
     Rigidbody mybody = go.AddComponent<Rigidbody>(); //add a rigid body
     mybody.mass =1; //add mass to the rigid body
     mybody.useGravity = true; //add gravity to the rigid body
     }

and it seems to work fine.

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

16 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

Related Questions

A node in a childnode? 1 Answer

How can I have an access to the rigidbody component of each instantiated object? 1 Answer

how can i make a List of rigidbody 1 Answer

c# reference site? need help figuring out storing rigidbodies into List<> 3 Answers

calculate the estimated distance travel based on the Force applied to Rigid Body 1 Answer


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