- Home /
Question by
KasaiKun · Jan 24, 2020 at 09:16 PM ·
collisiongameobjectobjectstag
How to effect a few gameobjects with the same tag through code?,How to affect multiple GameObjects with the same tag through code?
So I basicly came up with this script to disable the Box collider and the gravity on a GameObject, when picking up a powerup. In my game the player automaticly moves forward and must dodge obstacles along the way. The idea was that of making a ghost powerup, that allows him to pass through these obstacles for a certain time, but my code seems to affect only one of the obstacles instead them all . Any ideas on how to be able to effect all of them with the text Obstacle would be very welcome!
help-me-pls.png
(42.9 kB)
Comment
Best Answer
Answer by HAndLol · Jan 24, 2020 at 10:02 PM
add this:
//add this GameObject array to the top
private GameObject[] obstacles;
//add this when you want to ACTIVATE the effect
obstacles = GameObject.FindGameObjectsWithTag("obstacle"); //get all obstacles
foreach(GameObject obstacle in obstacles) //access them individually
{
obstacle.GetComponent<BoxCollider>().enabled = false;
obstacle.GetComponent<Rigidbody>().useGravity = false;
}
//add this when you want to DEACTIVATE the effect
obstacles = GameObject.FindGameObjectsWithTag("obstacle"); //get all obstacles
foreach(GameObject obstacle in obstacles) //access them individually
{
obstacle.GetComponent<BoxCollider>().enabled = true;
obstacle.GetComponent<Rigidbody>().useGravity = true;
}
hope this helps you bro :D
Answer by KasaiKun · Jan 25, 2020 at 08:57 AM
That was very helpfull and works just fine, thanks a lot I was really stuck there ^^