C# script, Image Resolutions
I have two questions basically - 1. I actually wanted to create a separate class where I can store all the game objects as static fields and use them throughout the game but since static fields do not show up in the inspector, I thought of doing GameObject.Find but i realized that this would take up a lot of time and also the InActive GameObjects won't be found. So, lastly I came up with creating a class where I can store all the GameObjects without marking them static and at the top of every other class where I need access to those GameObjects, I will create an instance of that class and use it throughout that class. Now, I am not sure if that's a good practice or not. Any suggestions on this would be hugely appreciated. 2. Can anyone tell me what's the standard file size, dimension and resolution for the UI Image Icons so that they fit perfectly in all the Android Devices having different resolutions? Looking forward for the answers. Thanks in advance.
Can I ask why you want to store all the gameobjects in a script?
Ordinarily I'd suggest just dragging any GameObjects to the inspector sheet to create references.
I do have a couple of arrays of GameObjects in my game, as I needed to grab all the event fighters, so I could loop through them all to find the closest one and set it as a target.
// this will give you 5 slots in the inspector window for dragging in your objects
public static List<GameObject> myGOs = new List<GameObject>(5);
//The below searches the array for a named gameObject
foundObject = myGOs.Find(x => x.name.Contains("objname"));
//Or if you want to perform an action on all objects with a certain tag
foreach (GameObject go in myGOs)
{
if (go.tag=="enemy")
{
[Do something with object tagged 'enemy' ]
}
}
It's much quicker searching a list than it is running a find across the whole scene.
This is just one of many ways to implement things and what you choose largely depends on what you are doing with the objects and why you want to store them in the first place?
@$$anonymous$$evRev thanks for your quick response mate....I actually have a need to use the same GameObject across various scripts so I felt like storing them at one place would be a lot easier than referencing the same across all the scripts. I am not quite sure about it that's why I have put this up... Also in your answer, I don't think that static list of gameObjects would show up in the inspector. " public static List myGOs = new List(5);"
You may have to drop the static, sorry, typing this on mobile without a code checker :)
That code should work. Are you using the objects for referencing actual scene objects, or as prefabs? If it's just for reference, the above code should work fine unless you are working with >10 gameObjects which will get messy. If so, you'd have to look at alternatives.
If the above doesn't work, can you describe a little about the gameobjects and what you are using them for?
Your answer
Follow this Question
Related Questions
storing multiple data types in a list 0 Answers
Remember UI setting from Previous Scene When Return to Scene 0 Answers
Accessing Variables inside classes inside other classes? 1 Answer
Can't get to work a non behaviour constructor with random generated variables 1 Answer
How do I create an undertale player health bar in C#? 1 Answer