how to autofill all gameobjects on scene automatically (Sprites) ?

hi all

I have Scenes 1,2,3,4,5 e.t.c and each sprite i adding manually.

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

}

thanks

You want to create an array:

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)

private ObjectScript[] objToLoad;

void Start()
{
objToLoad = FindObjectsOftype<ObjectScript>();
}

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.

Hope this was helpful :slight_smile:

I say you only THE BIG THANKS

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;

thanks for your time

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;

5257031--525545--upload_2019-12-7_12-17-31.png

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.

The other way you could use something like

private GameObject[] spriteFolder5;

void Start()
{
spriteFolder5 = Resources.LoadAll("sprites5", typeof(Sprite));
}

I think this should work. I think you will need to create a folder named Resources and drag you Sprites folder in it.

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

and BIG THANKS again

So they are prefabs?

Try this:

spriteFolder5 = new GameObject[Resources.LoadAll<GameObject>("spriteFolder5")];

sure pro developers tips for all newbies like me really helpful :slight_smile:

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

thanks for your time

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:
5257481--525611--upload_2019-12-7_15-38-37.png

The result in the scene is this:

5257481--525614--upload_2019-12-7_15-40-27.png
You can also see a different gizmo I added to the player’s weapon spawn, the red circle. I also made enemy spawn zones:

5257481--525617--upload_2019-12-7_15-41-53.png

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. :slight_smile:

there 5 gameobjects on the scene

before I press play I added all gameobjects manually

after I press play Yes I see in Obj To Load from your example all sprites which I add manually

2 animated files
//i.ibb.co/sFmPvt4/1.gif
//i.ibb.co/n30pX0j/2.gif

The main question:

That`s possible autoinsert all sprites from folder without any manual adding, for example from sprite folder ?

thanks for your time

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 code should work, amigo.

I try thanks

now I see only one easy way add gameobjects

public GameObject scenegameobjects;


and after manually moving to scenegameobjects
you can check what all your gameobjects placed like this

void Start ()
{
  scenegameobjects.SetActive (false);
}

big thanks for your tips

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.