Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

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

Discussion in '2D' started by motivision, Dec 6, 2019.

  1. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    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 ?
    Code (CSharp):
    1. using System.Collections;
    2. using UnityEngine;
    3.  
    4. public class LoadingGameObjects : MonoBehaviour {
    5.  
    6. 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;
    7.  
    8. //i don`t know how to do this
    9.  
    10. }
    thanks
     
  2. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    You want to create an array:

    Code (CSharp):
    1. private GameObject[] objToLoad; // Your array
    2. private int objCount = 0;
    3.  
    4. private void Start()
    5. {
    6. objToLoad = GameObject.FindGameObjectsWithTag("YourTag");
    7.  
    8. foreach(GameObject obj in objToLoad)
    9. {
    10. switch(objCount)
    11. {
    12. case 0:
    13. object1 = objToLoad[objCount];
    14. break;
    15. case 1:
    16. object2 = objToLoad[objCount];
    17. break;
    18. case 2:
    19. object3 = objToLoad[objCount];
    20. break;
    21. case 3:
    22. object4 = objToLoad[objCount];
    23. break;
    24. case 4:
    25. object5 = objToLoad[objCount];
    26. break;
    27. case 5:
    28. object6 = objToLoad[objCount];
    29. break;
    30. case 6:
    31. object7 = objToLoad[objCount];
    32. break;
    33. case 7:
    34. object8 = objToLoad[objCount];
    35. break;
    36. case 8:
    37. object9 = objToLoad[objCount];
    38. break;
    39. }
    40.  
    41. objCount++;
    42. }
    43.  
    44. }
    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:

    Code (CSharp):
    1. object1 = GameObject.FindWithTag("Obj1Tag");
    2. object2 = GameObject.FindWithTag("Obj2Tag");
    3.  
    4. // 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)

    Code (CSharp):
    1. private ObjectScript[] objToLoad;
    2.  
    3. void Start()
    4. {
    5. objToLoad = FindObjectsOftype<ObjectScript>();
    6. }
    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

    Code (CSharp):
    1. [HideInInspector]
    2. public GameObject // your object vars
    3.  
    4.  
    5. // or you could do this
    6. [SerializedField]
    7. private GameObject // Your object vars
    8.  
    9. // you could just make them all private
    10. 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 :)
     
  3. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    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
     
  4. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    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:

    Code (CSharp):
    1. public GameObject[] allSpritesArray;
    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
    Code (CSharp):
    1. private GameObject[] spriteFolder5;
    2.  
    3. void Start()
    4. {
    5. spriteFolder5 = Resources.LoadAll("sprites5", typeof(Sprite));
    6. }
    I think this should work. I think you will need to create a folder named Resources and drag you Sprites folder in it.
     
  5. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    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
     
  6. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    So they are prefabs?
     
  7. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    Try this:

    Code (CSharp):
    1. spriteFolder5 = new GameObject[Resources.LoadAll<GameObject>("spriteFolder5")];
     
  8. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    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


    thanks for your time
     
    Last edited: Dec 7, 2019
  9. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    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:
    upload_2019-12-7_15-38-37.png

    The result in the scene is this:

    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:

    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. :)
     
  10. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    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
     
    Last edited: Dec 8, 2019
  11. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    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.

    Code (CSharp):
    1. private int levelNum = 1;
    2. // We will increment this when the level is complete.
    3.  
    4. //Now you want to concatenate the levelNum to your string.
    5. //So if you have a folder inside Resource sprites1 it will call it
    6. //and load the sprites in that folder.
    7.  
    8. Resources.LoadAll("sprites"+levelNum); // Level 1 will always be levelNum = 1;
    9.  
    10.  
    11. //Whenever you level up you do this:
    12.  
    13. levelNum++;
    14.  
    15. //Now you access sprites2 folder in the Resource folder
    16. // Wouldn't be a bad idea to use PlayerPrefs at this point
    17. // and save levelNum and on Start() of each level access it.
    18.  
    19. //Where ever you control leveling up, add this code:
    20. PlayerPrefs.SetInt("LevelNum", levelNum);
    21.  
    22. // On start up of level do this:
    23.  
    24. private void Start()
    25. {
    26. levelNum = PlayerPrefs.GetInt("LevelNum");
    27. }
    28. //Just keep in mind when you quit game to reset levelNum to 1
    29. levelNum = 1;
    30. PlayerPrefs.SetInt("LevelNum", levelNum);
    This code should work, amigo.
     
    Last edited: Dec 8, 2019
  12. motivision

    motivision

    Joined:
    Dec 3, 2019
    Posts:
    28
    I try thanks

    now I see only one easy way add gameobjects
    Code (CSharp):
    1. public GameObject scenegameobjects;

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

    Code (CSharp):
    1. void Start ()
    2. {
    3.   scenegameobjects.SetActive (false);
    4. }

    big thanks for your tips
     
  13. MisterSkitz

    MisterSkitz

    Joined:
    Sep 2, 2015
    Posts:
    833
    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:

    Code (CSharp):
    1. public GameObject[] sceneGameObjects;
    2. // Create an array and drag all images into the inspector
    3. // You have 5 images so create 5 elements
    4.  
    5. private void Start()
    6. {
    7. foreach(GameObject obj in sceneGameObjects)
    8. {
    9. // Set all items in the array to false on Run
    10. obj.SetActive(false);
    11. }
    12. }
    My fault, I misunderstood what you wanted the entire time.