Anybody know how to do this automatically through script like LoadingGameObjects ?
using System.Collections;
using UnityEngine;
public class LoadingGameObjects : MonoBehaviour {
public GameObject object1, object2, object3, object4, object5, object6, object7, object8, object9, object10, object11, object12, object13, object14, object15, object16, object17, object18, object19, object20, object21, object22, object23, object24, object25;
//i don`t know how to do this
}
private GameObject[] objToLoad; // Your array
private int objCount = 0;
private void Start()
{
objToLoad = GameObject.FindGameObjectsWithTag("YourTag");
foreach(GameObject obj in objToLoad)
{
switch(objCount)
{
case 0:
object1 = objToLoad[objCount];
break;
case 1:
object2 = objToLoad[objCount];
break;
case 2:
object3 = objToLoad[objCount];
break;
case 3:
object4 = objToLoad[objCount];
break;
case 4:
object5 = objToLoad[objCount];
break;
case 5:
object6 = objToLoad[objCount];
break;
case 6:
object7 = objToLoad[objCount];
break;
case 7:
object8 = objToLoad[objCount];
break;
case 8:
object9 = objToLoad[objCount];
break;
}
objCount++;
}
}
That’s assuming they all have the same exact tag. If each has their own unique tag then all you’d need to do is:
object1 = GameObject.FindWithTag("Obj1Tag");
object2 = GameObject.FindWithTag("Obj2Tag");
// for all of them
You could also create a new blank C# script and name it something like ObjectScript. Then you can locate all of them regardless of their tag like this: (You will put the blank script on each of the game objects you want to find)
Once you locate your objects, you can then use the elements to initialize your objects variables. If you use this method, you may not need your variables to be visable inside the inspector so you can utilize
[HideInInspector]
public GameObject // your object vars
// or you could do this
[SerializedField]
private GameObject // Your object vars
// you could just make them all private
private GameObject // your obj vars
Really depends on whether you want to be able to access these variables from other scripts or not.
And another small question.
For example i have on Scene 5 25 sprites which in folder sprites5
that`s possible automatically load all sprites from this folder to public GameObject[ ] objToLoad;
The easiest way to achieve this would be to create a public array[ ] and just add how many elements then drag and drop them in the inspector like this:
public GameObject[] allSpritesArray;
You just set the size then drag all of your sprites in there. You can access them anytime you need to. Since you know exactly which element each on is, you can reference specific ones by index easily.
all my sprites have if conditions with drag and drop functions
but your solution really helpful for static sprites which only like a graphic on the scene
I try follow your solution for static sprites on the scene
sure pro developers tips for all newbies like me really helpful
with loading folder your tips really cool
So they are prefabs?
no just gameobjects, me interesting that`s possible or not autofill gameobjects like a sprites from folder or from stage
Try this:
ok I try again because see some errors
Will be great if developers make cool features where you can autofill gameobjects (all selected gameobjects on scene with some marked colors for example or with gameobject tag), just selected with marked colors or gameobjects tags and they all imported. just thinking out loud
You should look into Gizmos because these will provide color for your game objects within the scene view by default. However, you can set it to where they are visible in the Game view as well.
Here’s an empty object that I added a gizmo to for visibility purposes:
The result in the scene is this:
You can also see a different gizmo I added to the player’s weapon spawn, the red circle. I also made enemy spawn zones:
The yellow circles are where the enemy can spawn.
I’m not sure why you would need to auto load objects that are already in the game though. The process to detect them already exist. If you have these “highlighted” game objects, then they have already loaded. If you detect a game object, you can get the Transform.position and use Instantiate() to make your objects appear. However, these would need to be prefabs.
Anyway, share the error messages with me so I can see what the problem is and fix it.
Show me the code you are currently using. I do not see a Resources folder in your project. Your Sprites folder needs to be inside a folder called Resources if you’re using the Resources.LoadAll(). But yes, That’s how you load all of your files on Run: Resources.LoadAll(“Sprites”). You need to access the Resource file>>Then reference the folder “Sprites” as a parameter.
So let’s say every level you have a different sprite folder. Simply call that folder.
private int levelNum = 1;
// We will increment this when the level is complete.
//Now you want to concatenate the levelNum to your string.
//So if you have a folder inside Resource sprites1 it will call it
//and load the sprites in that folder.
Resources.LoadAll("sprites"+levelNum); // Level 1 will always be levelNum = 1;
//Whenever you level up you do this:
levelNum++;
//Now you access sprites2 folder in the Resource folder
// Wouldn't be a bad idea to use PlayerPrefs at this point
// and save levelNum and on Start() of each level access it.
//Where ever you control leveling up, add this code:
PlayerPrefs.SetInt("LevelNum", levelNum);
// On start up of level do this:
private void Start()
{
levelNum = PlayerPrefs.GetInt("LevelNum");
}
//Just keep in mind when you quit game to reset levelNum to 1
levelNum = 1;
PlayerPrefs.SetInt("LevelNum", levelNum);
This whole time you were only trying to hide your images on Run? I thought you were trying to load them from the project folder. If that’s all you wanted to do you can do this:
public GameObject[] sceneGameObjects;
// Create an array and drag all images into the inspector
// You have 5 images so create 5 elements
private void Start()
{
foreach(GameObject obj in sceneGameObjects)
{
// Set all items in the array to false on Run
obj.SetActive(false);
}
}
My fault, I misunderstood what you wanted the entire time.