Resources.load ?

Can the call resource.load be used more than once in a function. I am trying to use it to fill a list but only the first resource.load seems to be working, in the code below, if i run it, fishShadow will load but sharkShadow will not. But if i replace fish with shark, then it does load shark but not fish.

Can it be used this way?

public List<GameObject> shadows;

    void loadLevelOne()
    {
        Debug.Log("Level One");
        shadows[0] = (GameObject)Instantiate(Resources.Load("fishShadow"));
        shadows[0].transform.position = anchorPoint[0].transform.position;
        shadows[1] = (GameObject)Instantiate(Resources.Load("sharkShadow"));
        shadows[1].transform.position = anchorPoint[1].transform.position;
    }

Short answer: no.

Long answers:
1; forget Resources. Use Addressables.
2; if you are your own enemy, use LoadAll: https://docs.unity3d.com/ScriptReference/Resources.LoadAll.html

Thanks I will look into Addressables. But I wonder, is the reason it is not working because I am trying to fill a list? I just noticed that another list that I was trying to fill wasnt actually working, it would only do the first element and then the rest of the code would skip. Is this the same reason?

Could be anything, here are three things that come to mind:

  • you misspelled sharkShadow
  • the thing isn’t even called sharkShadow
  • it’s loading something ELSE that isn’t a GameObject but is called sharkShadow

And plenty more.

The final one has to do with your not typing your function.

ALWAYS use this form: Resources.Load<T>();

such as:

GameObject prefab = Resources.Load<GameObject>( "fishShadow");

If do not use the variant, and if you have a fishShadow.png and a fishShadow.fbx, it’s a 50/50 chance which one you’ll get. Without the specifier, it might load the PNG, then try to instantiate it, which it might, then cast it to a GameObject and throw an error.

ALWAYS use the variant.

So should i be able to use the list the way i am trying, i.e list[n] = something ?

If I am trying something like the code below, should that work with a list (anchorPoint is a list) because right now, when i try and use a list like this it runs the code of the first then then skips the rest. I have tried this with different lists (the code from previous post, and code below) and it is always skipping the remaining code?

        anchorPoint[0] = GameObject.Find("anchorPoint1");
        anchorPoint[1] = GameObject.Find("anchorPoint2");

If by that “something” you mean Resources.Load<T>( "myAssetPath"); , then yes.

Wow, you ARE living dangerously @ChuckieGreen !! Not only are you using Resources.Load but you’re also proposing to use GameObject.Find! Wow… I would highly recommend staying away from both of these constructs.

Don’t get me wrong, both of them CAN be made to work, but both of them come with a huge list of caveats and not-fully-defined requirements before they will work, far too numerous to list here, but they’re all over the internets.

I am going to look into the addressables that lurking-ninja suggested rather than the resources.load, I was just wanting to figure out why the code was not working for resource.load and the achorPoint list first, before changing the code to work with the addressables. I just cant seem to figure out why the code is skipping, especially now that you have said it should work.

I will only be using the find on 5 objects in the entire scene, do you think I should still use something else rather than find? Or has it got more to do with having to search through all the game objects to find what I am actually looking for?

using System.Collections.Generic;
using UnityEngine;

public class Test : MonoBehaviour
{
    public List<GameObject> shadows = new List<GameObject>();
    public List<GameObject> anchorPoint;
   
    void loadLevelOne()
    {
        Debug.Log("Level One");
        shadows.Add(Instantiate(Resources.Load<GameObject>("Cube")));
        shadows[0].transform.position = anchorPoint[0].transform.position;
        shadows.Add(Instantiate(Resources.Load<GameObject>("Sphere")));
        shadows[1].transform.position = anchorPoint[1].transform.position;
    }

    private void Awake()
    {
        loadLevelOne();
    }
}

7122362--850496--screenshot1.png
7122362--850493--screenshot2.png
7122362--850487--screenshot3.png
7122362--850490--screenshot4.png

2 Likes

Just got back into being able to try this, and it is now working. Just wanted to say thanks for the help