- Home /
Displaying a single gameObject from an array of gameObjects
So I have an array of game objects, which I attached to a gameObject within the bounds of my camera:
public GameObject[] storeAppliances;
The array is manually filled in from the inspector window. In the start function, this is what I have:
for (int i = 0; i < storeAppliances.Length; i++)
{
storeAppliances[i].active = false;
}
When you click on a button in the game, I have it so:
GameObject displayItem = (GameObject)Instantiate(storeAppliances[arrayLoc], new Vector3(0,2,0), transform.rotation);
displayItem.active = true;
(arrayLoc is determined by the button you press in a list of buttons, which is the same length as storeAppliances).
After another button is pressed, I change it so:
displayItem.active= false;
I can't get the gameObject to display on the screen, and according to my Hierarchy, it isn't instantiating. I'm fairly new to unity, so I'm not sure where my mistake is made. All of the buttons are doing what they should be doing, so there is no issue there, but my code that I added to make an gameObject display on the screen is not working. If anyone could help, I would GREATLY appreciate it.
you cant work on disabled gameobjects.they wont instantiate or cannot be found using GameObject.Find().Ins$$anonymous$$d of setting the item to inactive work with the meshrenderer to hide/show it.
for (int i = 0; i < storeAppliances.Length; i++)
{
storeAppliances[i].renderer.enabled=false;
}
Dont make the object inactive if you want to use it later or you have to set it active before using.
If you dont want any object to be used again,Destroy it for better performance dont hide or make it inactive
Your answer
Follow this Question
Related Questions
Javascript How to Activate/Deactivate GameObject Arrays 1 Answer
A node in a childnode? 1 Answer
Modifying A*Pathfinder... 1 Answer
C# SetActive GameObject Array 2 Answers
Unity 3 GD HotShot-Tutorial Problem with an C#-Array 1 Answer