I’ve been trying to make a list of classes for making each class/factory have it’s unique level. I can’t, because it says that I can’t use “new” to add a MonoBehavior class to the list. I can’t understand how to use the alternative, AddComponent(), either.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Spawning : MonoBehaviour
{
public static Vector3 trueMousePos;
private int id;
List<Factory> factories = new List<Factory>();
void Update()
{
Vector3 worldPoint = Camera.main.ScreenToWorldPoint(Input.mousePosition, Camera.MonoOrStereoscopicEye.Mono);
trueMousePos = new Vector3(worldPoint.x, worldPoint.y, transform.position.z);//this is for finding the position of the mouse in the game world
if (Input.GetKeyDown(KeyCode.Alpha1)) { id++; factories.Add(new Factory(id)); }//the error is here
}
}
public class Factory : MonoBehaviour
{
public GameObject factory;
private int id;
private int level;
public Factory(int id)
{
this.id = id;
}
void Start()
{
Instantiate(factory).transform.position = Spawning.trueMousePos;
}
void OnMouseDown()
{
if (Input.GetKeyDown(KeyCode.Mouse0)) { this.level++; variables.tac--; }
if (Input.GetKeyDown(KeyCode.Mouse1)) { this.level--; variables.tac++; }
}
}