Disabling all rigidbodies with a special tag

Hey guys! I’m in the process of creating a 2.5D sidescroll platform, and in the process, I have to address the gravity component of every rigidbody, with a special tag.

How do I broadcast a message, that goes directly for the tag, with me being able to say that “every Gameobject with this tag”.GameObject.rigidbody.useGravity = false; ?

Hope you’re able to call out my problem!

Thanks in advance, Trixxr.

I would recommend just making a list of the game objects and then run through that list. It will be faster than searching on the tag. There is a method for finding a game object by tag, but its not particularly fast and I wouldn’t recommend using it while the game is playing. I would recommend maintaining your own list or doing this once as your level loads!

static function GameObject.FindGameObjectsWithTag (tag : String) : GameObject[]

Cheers,
UnLogick

My problem is, that I’m instantiating objects on the run, that needs the broadcast message - which makes it hard to “add it to a list”.

It’s not gonna be run a lot, only once like every 10 second (at max). and there’s only gonna be around 10 objects (at the very max) with that tag.

I’ll try your solution though! Thanks a lot.

To set you into the situation :

I’m spawning boxes, that fall from the sky, when I’m in a special mode. While in this mode, all gravity is turned off, in order to be able to position the boxes in different places, and then enabling gravity, which should solve the puzzle.
However, there’s no way for me to stop the gravity component, the next time i enter the mode, for the boxes that were spawned previously - as I have no way to call the gravity component for the new ones.

So, when the box is fell off, you can stop the gravity (which mean, all boxes are stopped), right?
Then you can checking the Gravity mode while you Instantiating the boxes, right.

In your prefab gameobject, put a script that saying: “Oh, I’m being created. Let’s check my world state. Oh! The Gravity is off, then I should stop moving now!”

So, ,may be you can write like this (assuming that you only freezing one type of box, and box only in your game):
Worldstatus.js <-attach this script in your camera. Or any object that will last forever during the game

static var ZaWarudo=false; //This is your main world status.

Gravitychecker.js <-attach this script in your prefab box.

function Update(){
if (Worldstatus.ZaWarudo == true){
//Your gravity disabling script here
}
}

This code will check every frame for ZaWarudo status inside Worldstatus.js
If ZaWarudo is true, then the box will be freezed, including the new one because when a new box appear, this code will be executed right away.

But if you want to disable the gravity for certain number of boxes (like only 3 from 10 boxes), that will be different case.