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.