[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.

1 Answer

1

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.