So I’ve been doing some OO in Visual Studio using C# and have decided to try my hand at classes in Unity but have come across a few issues.
Firstly - “You are trying to create a MonoBehaviour using the ‘new’ keyword. This is not allowed. MonoBehaviours can only be added using AddComponent(). Alternatively, your script can inherit from ScriptableObject or no base class at all
UnityEngine.MonoBehaviour:.ctor()”
That is the warning I get when I try to initialize a class, here is my code to initialize -
public Transform myTransform;
// Use this for initialization
void Awake () {
ClassTest imATest = new ClassTest("Donkey", "Fish", "Mule", "Meow", myTransform);
}
Here is the actual class -
public class ClassTest : MonoBehaviour {
private string animal1;
private string animal2;
private string animal3;
private string sound;
private Transform myTransform;
//Constructor
public ClassTest(string animal1, string animal2, string animal3, string sound, Transform myTransform)
{
this.animal1 = animal1;
this.animal2 = animal2;
this.animal3 = animal3;
this.sound = sound;
this.myTransform = myTransform;
}
Now, thats only a warning but I get an actual error when I try to instantiate the transform in a method. - Only assignment, call, increment, decrement, and new object expressions can be used as a statement
Here is my method for the instantiation -
public void Spawn()
{
Instantiate(myTransform, gameObject.Find(spawnArea).transform.position, Quaternion.identity) as Transform;
}
Obviously I’m very new at all this so any help would be much appreciated!
Cheers.