- Home /
Controlling prefabs from Resources Folder
I have loaded a bunch of prefabs which have objects on the scene. Now I choose to randomly disable one from the script. As the object on the scene is attached to the prefab and the prefab is controlled by the script, I assumed I could setActive (false). This didn't work. Is there a way to disable the prefab set in the resources such that it reflects on the scene.
public GameObject[] tableObjects;
void Start ()
{
tableObjects = Resources.LoadAll<GameObject> ("GameObjects");
for (int i=0; i<objCount; i++)
{
tableObjects[i].SetActive (true);
}
}
void SelectRandomObjects(int GameLevel) {
for (int i = 1; i <= GameLevel; i++) {
int x = Random.Range(0,30);
if (tableObjects[x].activeSelf == false)
i--;
else
{
tableObjects[x].SetActive (false);
/* This disables the prefab but only when I load the scene again does it appeared disabled */
} } }
Answer by browne11 · Apr 08, 2016 at 02:18 PM
@adichav Perhaps you can Destroy (gameObject); then instantiate the object back when needed unless you're trying to store info in that object.
Or try to enable/disable the script or component with this line of code. gameobject.GetComponent< SomeScript >().enabled = true;
Let me know if that helps.
@Cover Club $$anonymous$$edia
Ill explain the game slightly so you may get a better idea. I am disappearing objects statically placed in the scene. The number of objects disappearing is based on the level. There are toggles for the user to use, so that they may be able reappear the object and should they find all the objects they beat the level. Now I am trying to dynamically obtain all the objects and create a toggle for them, so that if I add a new object to the scene and add its prefab to the resources folder, I should be able to use it.
Couldn't use either,
The Destroy(gameObject) was not friendly as there are close to 30 objects to disappear and reappear, each with different locations. During some levels, as many as 10 objects disappear and they have to reappear upon clicking on the options. Furthermore, destroying the object would mean that I couldn't check again the options available to user.
I tried disabling the mesh of the game object, but what happens is that only after the game stops (ending Play-mode) does the mesh component turn off.
Hope the info helps you to help me :D
@adichav The mesh route shouldn't have that desired effect but it doesn't sound like the solution to your problem.
Did you try object pooling? You have something similar. If you have a return statement that would return the false status properly. Sounds like that would be what you need. In the below video he uses it for bullets which will add up also. http://unity3d.com/learn/tutorials/modules/beginner/live-training-archive/object-pooling
public class ObjectPoolScript : $$anonymous$$onoBehaviour
{
public GameObject pooledObject;
public int pooledAmount = 20;
public bool willGrow = true;
public List<GameObject> pooledObjects;
void Start ()
{
pooledObjects = new List<GameObject>();
for(int i = 0; i < pooledAmount; i++)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects.Add(obj);
}
}
public GameObject GetPooledObject()
{
for(int i = 0; i< pooledObjects.Count; i++)
{
if(pooledObjects[i] == null)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
obj.SetActive(false);
pooledObjects[i] = obj;
return pooledObjects[i];
}
if(!pooledObjects[i].activeInHierarchy)
{
return pooledObjects[i];
}
}
if (willGrow)
{
GameObject obj = (GameObject)Instantiate(pooledObject);
pooledObjects.Add(obj);
return obj;
}
return null;
}
}
Hopefully that helps
Your answer
