Hey there !
Okay so my player can pickup some items called “Pickup”, “Pickup (1)”, etc. to “Pickup (4)”.
If he touch one of these I set the gameobject on false and count it. But if my player die I want to put back these items into the game.
Here’s my code.
public void ResetMap() {
GameObject[] array = new GameObject[5];
array = GameObject.FindGameObjectsWithTag ("PickUp");
foreach (GameObject pick in array) {
pick.SetActive (true);
}
And it doesn’t work… I don’t really know how to do, a bit of help please ?
Please note that my pickups are childs of a “Pickups” GameObject that is empty and untagged.
I think the problem is that the FindGameObjectsWithTag method doesnt return inactive GameObjects.
What you could do is add the PickUps to a List just before they get deactivated, then iterate over that list when you want to reactivate them. clear the list afterwards.
FindGameObjectsWithTag returns only those GameObjects which are active. It doesn’t return inactive ones. The reason why you cannot make your pickups active anew is that the “array” variable is empty. One way to solve this problem would be to make the “array” variable of GameObjects global and fill it with GameObjects in Awake method (assuming of course that all pickup objects are active at the start of the game):
// make any script and attach for the gameObject do you want to control with it
//and this script
" here the nameof script" pickups = (“here the nameof script”)FindObjectsOfType(typeof(“here the nameof script” )) ;
foreach ("here the nameof script "pickup in pickups)
{
pickup.gameObject.SetActive( false);
}
//
foreach ("here the nameof script "pickup in pickups)
{
pickup.gameObject.SetActive( false);
}
Many thanks to everyone who answered me ! I’ll try this right now but I think it will work !
I’ll accept an answer when done !
EDIT : Okay so it works perfectly for the first level, but then I change scene (with the same player game object, that is kept throught levels), the references of objects doesn’t update…
I know that’s kind of another problem, but I already had this kind of issues before and nobody answered
I tried to re-use the line