- Home /
Question by
HamzaAlrawi · Jun 28, 2018 at 09:05 AM ·
c#classmonobehaviour
You are trying to create a MonoBehaviour using the new keyword. This is not allowed
Hello,
So I have seen this problem before but my class is not a child of MonoBehaviour! Shouldn't this not happen?
Here's (shortly) the code:
public class FirstBossHeadScript : MonoBehaviour {
public EnemyStats thisenemy = new EnemyStats();
public void Start()
{
thisenemy.maxHealth = 10;
}
}
Here's the EnemyStats class:
public class EnemyStats {
public int maxHealth = 100;
}
Comment
Are you sure you don't have multiple classes called EnemyStats
?
Try to :
Close Unity
Delete the
Library
folder next to yourAssets
folderOpen Unity again
Best Answer
Answer by dev-waqas · Jun 28, 2018 at 10:16 AM
Try moving new inside the start function like this.
public class FirstBossHeadScript : MonoBehaviour {
public EnemyStats thisenemy ;
public void Start()
{
thisenemy = new EnemyStats();
thisenemy.maxHealth = 10;
}
Answer by madks13 · Jul 13, 2018 at 02:33 PM
Umm, just stumbled upon this. The error message is because Unity doesn't Allow creation of MonoBehaviour classes with the keyword new. Instead use GameObject.Instantiate.