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; ?
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[]
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.