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.
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?
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.
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?
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();
}
}