Unity returns InvalidCastException when attempting to run a method from another file

Okay, so. Everything was going smoothly until I ran into this one peculiar problem, that the internet does not seem to have an answer for. This is my main script that calls stuff from other scripts.

using UnityEngine;
using baseFunct;
using mobFunct;
using mapFunct;

[RequireComponent(typeof(AudioSource))]
public class gm : MonoBehaviour
{
    public float hor;
    public float ver;

    public void Start()
    {
        Vector3 startpos = new Vector3(1, 1, 0);
        World testworld = mapf.genWorld("testworld", 4);
        mapf.renderCell(testworld.cell[new Vector3(0, 0, 0)]);
        mapf.derenderCell(testworld.cell[new Vector3(0, 0, 0)]);
        Mob player = new Mob();
        player = mobf.spawnMob("Player", startpos);
    }

    void Update()
    {
        hor = Input.GetAxisRaw("Horizontal");
        ver = Input.GetAxisRaw("Vertical");
        if (Input.anyKey)
        {
            //Do a thing
        }
    }
}

At the line

player = mobf.spawnMob("Player", startpos);

It seems to die. I get an “InvalidCastException: Cannot cast from source type to destination type.”

This is the file that contains the method:

using UnityEngine;
using mapFunct;

namespace mobFunct
{
    public class Mob  //define our base mob
    {
        public string name = "unnamed";
        public Vector3 worldpos = new Vector3(0,0,0);
        public GameObject model = new GameObject();

        public bool moveTo(World t_world, Vector3 t_pos)
        {
            if (t_world.block[t_pos].is_open) {
                worldpos = t_pos;
                model.transform.position = t_pos;
                return true;
            } else { return false; }
        }
    }



    public class mobf : MonoBehaviour
    {
        static public Sprite[] m_sprite = (Sprite[])Resources.LoadAll("gfx/mob");
        static public GameObject genericmob = (GameObject)Resources.Load("genericmob");

        static public Mob spawnMob(string newname, Vector3 newpos)
        {
            Debug.Log(newname);
            Debug.Log(newpos);
            Mob newmob = new Mob();
            newmob.name = newname;
            newmob.model = (GameObject)Instantiate(genericmob,newpos,Quaternion.identity);
            newmob.model.GetComponent<SpriteRenderer>().sprite = m_sprite[1];
            return newmob;
        }
    }


}

As far as I can tell, there is literally no reason I should be getting this error. It happens during run-time, Visual Studio doesn’t find anything.

I am not casting. I have examined another file where I attempt to call a similar method. Everything is done the exact same way. I have tried commenting out the contents of the method and making it a public static void. Totally empty. And it returns the same exception. I then tried copypasting it (still empty) to mapf, which contains my mapFunct namespace. It worked fine.

Are you sure the exception doesn’t happen somewhere inside mobf.spawnMob? Looks odd to me. Also looks odd that you create a mob that you then toss away Mob player = new Mob(); in gm.Start but that shouldn’t matter. What I can see could possibly go wrong is:

static public Sprite[] m_sprite = (Sprite[])Resources.LoadAll("gfx/mob");
static public GameObject genericmob = (GameObject)Resources.Load("genericmob");

These initializers will be called when the class is initialized. Please initialize the static variables in a method so you more easily can debug if either of these casts fail. I would guess that your Sprite cast will fail if gfx/mob contain anything else than Sprites for example.

If the call to mobf.spawnMob is the first time you access mobf, then it would make sense that either of those could fail.

Ah, thank you so much for your speedy and insightful response, Statement. Thanks to your suggestion (and a nap) I was able to find a way around the problem.

I decided to use a list to handle the loading of sprites. Will probably work better that way anyways.

The problem was connected to those two lines, as you suggested. I moved the genericmob declaration inside my spawnMob method, and incorporated the filling out of a list into a method of it’s own while also adding a type to Resources.Load, as shown here. It worked like a charm.

    public class mobf : MonoBehaviour
    {
        static public List<Sprite> m_sprites = new List<Sprite>();

        static public void loadSprites()
        {
            m_sprites.Add((Sprite)Resources.Load("gfx/mob/prot_hero",typeof(Sprite)));
        }

        static public Mob spawnMob(string newname, Vector3 newpos)
        {
            GameObject genericmob = (GameObject)Resources.Load("genericmob");
            Debug.Log(newname);
            Debug.Log(newpos);
            Mob newmob = new Mob();
            newmob.name = newname;
            newmob.model = (GameObject)Instantiate(genericmob,newpos,Quaternion.identity);
            newmob.model.GetComponent<SpriteRenderer>().sprite = m_sprites[0];
            return newmob;
        }
    }