- Home /
How to reactivate items ?
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.
Have a nice end of day !
Answer by yummy81 · Feb 06, 2018 at 06:54 PM
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):
private GameObject[] array;
private void Awake()
{
array = GameObject.FindGameObjectsWithTag("PickUp");
}
private void ResetMap()
{
foreach (GameObject pick in array)
{
pick.SetActive(true);
}
}
Answer by Hurri04 · Feb 06, 2018 at 06:46 PM
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.
yes you're right.I forgot that we cannot find an object that is inactive
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : $$anonymous$$onoBehaviour {
public List<GameObject> objects;
public int counter;
void Start () {
StoreObjcets();
}
void StoreObjcets(){
GameObject[] gameObjects = GameObject.FindGameObjectsWithTag ("Ground");
for(var i = 0 ; i < gameObjects.Length ; )
{
objects.Add (gameObjects [i]);
gameObjects [i].SetActive (false);
i++;
counter++;
}
}
public void activeObjcets(){
for(int a=0;a<counter;){
objects [a].SetActive (true);
a++;
}
}
}
just like this @Nucl3ear ,put objects in a list then deactive them. :))
Answer by josh15 · Feb 06, 2018 at 06:52 PM
// 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);
}
i hope its work
Answer by Nucl3ear · Feb 06, 2018 at 07:16 PM
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
array = GameObject.FindGameObjectsWithTag ("PickUp");
But it seems like the array isn't updating.. Any idea ?
Many thanks !
That line should refresh the array, but where have you put it? It needs to be called again once the new scene has loaded. See eg the answer to this question for more: https://answers.unity.com/questions/1295851/event-when-the-scene-is-loaded.html
$$anonymous$$any thanks I'll swarch that way and close this topic.
Thanks to everyone ! :D You rock guys !
Your answer
Follow this Question
Related Questions
how to change a GameObject from another script 1 Answer
Panel GameObject not activating 0 Answers
Panel GameObject not activating when called from another script 1 Answer
How to detect an object which be in FOV of certain camera ? 1 Answer
Changing TPP view to a FPP view after picking up object 1 Answer