Saving gameObjects in a list, then istantiate them randomly

Hey everyone! I’m currently doing Ben Tristems unity developers course on Udemy and I decided to go above and beyond (Because thats how you learn right?) Well for tjhose of you familiar with his course I’m trying to spawn a random enemy from a list into the game. However after a few hours of research and tinkering I got to the point where I thought it would work! Now I run it and my gameObject to be loaded says its null. I’m sure this is probably a simple fix I just can not seem to figure out what it is.

Here is what I have so far.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EnemySpawner : MonoBehaviour {
    List<GameObject> EnemiesArray;
    GameObject RandomEnemy;
    GameObject SpriteToLoad;
    bool full = false;
    int count = 0;


    void Start(){
        EnemiesArray = new List<GameObject>();
        while (!full)
        {
            SpriteToLoad = Resources.Load("Assets/Resources/Enemies") as GameObject;
            EnemiesArray.Add(SpriteToLoad);
            count++;
            print(count);
            print(SpriteToLoad);
            if (EnemiesArray == null || EnemiesArray.Count <= 1)
            {
                full = true;
            }
            else
            {
                full = false;
            }
        }
    }
    // Update is called once per frame
    void Update() {
            RandomEnemy = EnemiesArray[Random.Range(0, EnemiesArray.Count)];
            Vector3 NewLoc = new Vector3(0, 0, 0);
            GameObject NewSpawn = Instantiate(RandomEnemy, NewLoc, Quaternion.identity);
    }

}

So a few things:

  • EnemiesArray is never null because you initialize it 5 lines before you check
  • That loop will only add 1 enemy to the list
  • What is count for?
  • My guess is that SpriteToLoad comes back as null from your Resources.Load call and you’re just adding null to the list. Your path is probably wrong

Resources.Load

Parameters
path Pathname of the target folder. When using the empty string (i.e., “”), the function will load the entire contents of the Resources folder.

Make sure to put your assets to Resources folder and use path relative to it. For example, if your asset is in “Resources/Assets/Textures”, then you Load(“Assets/Textures”). In your case it’s probably Load(“Enemies”)?

Hey guys thanks for the quick replies! I definitely had the wrong path, the way I read the API for it made it seem like I had to create a resources folder INSIDE my assets folder. Count was just for debugging purposes really. However I am confused as to why that would only add one of my four prefabs to the list? Current issue is the object I want to instantiate is null. I was trying to go for being able to easily expand the code for later use while writing this, not sure if I accomplished that though.

EDIT: Yep its definitely the SpriteToLoad coming back null.
The folders go Assets to PreFabs to Enemies and all enemy prefabs are inside that folder.
My current path is Resources.Load(“Assets/PreFabs/Enemies”)
That should be the correct path if I’m understanding this correctly

You say while full is false and then set full to true when EnemiesArray.Count is less than or equal to 1 and it equals 1 as soon as you add the first item.

I was attempting to avoid an infinite loop, I thought that is what was causing the editor to crash when I hit run.

I apologize if I am missing the obvious, it could be time to take a little break and come back to it lol, I’ve been doing this course for like 12 hours straight…

You could skip the bool checks

// make a list of 7 enemies
while (EnemiesArray.Count != 7)
{

}

Okay I’ll try it out here in a little bit and see what happens! So really if I wanted to use this script in a different project I would just need to change the path and the while loop exit number if I’m not mistaken. Thank you!

Okay so I checked and check again. My current block of code is this to load the enemy PreFabs
LoadSprites(); is the very first thing done by this script at start.

  List<GameObject> EnemiesArray;
  GameObject RandomEnemy;
  GameObject SpriteToLoad;
    void LoadSprites()
    {
        EnemiesArray = new List<GameObject>();
        while (EnemiesArray.Count != 4)
        {
            SpriteToLoad = Resources.Load("enemies") as GameObject;
            EnemiesArray.Add(SpriteToLoad);
            print(SpriteToLoad);
        }
    }

My path for this folder is as follows.
Assets > Resources > Enemies

I’ve tried the blank string directly in resources, I tried with and without the assets part of the string, I’ve tried just about every way I can think of to make the string match up. No matter what I do nothing works, I even tried instantiating each object after loaded but before adding to EnemiesArray.
Can someone please explain to me what I’m still doing wrong?

The error in unity says object you wish to instantiate is null which makes sense that what SpriteToLoad is printing out as, but why? Even if it was saving them all in the same spot it should print at least something other than null one time.

Your Load call is failing. It should be a path to an asset not a path to a folder. Also - “enemies” isn’t the same thing as “Enemies” :slight_smile: