Hi, I am learning to use inheritance and trying to use it in my current project. I have a BasicMob class that defines the properties of my basic mobs, then I have the spider class which inherits from basicmob. Then I have a MobManager class that is attached to a GameObject in the scene and instantiates the mobs into the game.
One of the properties of my “basic mob” is the prefab. I am trying to use a property instead of just a standard variable. I am being told that the prefab is null though when I instantiate. Here is my code, is it possible to do this or do I maybe have my syntax wrong?
using UnityEngine;
using System.Collections;
public class MobManager : MonoBehaviour {
public Transform[] spawnPoints;
// Use this for initialization
void Start ()
{
SpawnMob(spawnPoints[0].position, MobType.spider);
}
// Update is called once per frame
void Update () {
}
public BasicMob CreateMob(MobType mobType)
{
BasicMob mob = new BasicMob();
if(mobType == MobType.spider)
{
BasicMob spider = CreateSpider();
spider.Name = "Spider";
spider.AttackDamage = 10;
spider.DefaultMoveSpeed = 10;
spider.StartHealth = 100;
spider.MobPrefab = Resources.Load("Prefabs/Spider") as GameObject;
spider = mob;
}
return mob;
}
private Spider CreateSpider()
{
Spider spider = new Spider();
return spider;
}
void SpawnMob(Vector3 spawnPoint, MobType mobType)
{
//CreateMob(mobType);
Instantiate(CreateMob(MobType.spider).MobPrefab, spawnPoint, Quaternion.identity);
}
}
The error comes back on this line:
Instantiate(CreateMob(MobType.spider).MobPrefab, spawnPoint, Quaternion.identity);
Here is the error message:
ArgumentException: The prefab you want to instantiate is null.
You gotta be kidding me the only problem was mob = spider instead of spider = mob.. lol :D Thanks.
– madmike6537