How to create a class and save a list of that object?
I need to create a class that saves me 2 numbers and 1 string, I tried to create it like this:
public class Coordenadas : MonoBehaviour
{
public string mov;
public double cooX;
public double cooy;
public Coordenadas(string mv, double cX, double cY)
{
mov = mv;
cooX = cX;
cooy = cY;
}
}
So when I try to create a list of this object in a script I do it like this:
public List<Coordenadas> playerLevelData = new List<Coordenadas>();
So I try to create the object and add it to the list:
Coordenadas mov = new Coordenadas("D",Math.Round(transform.position.x),Math.Round(transform.position.y));
playerLevelData.Add(mov);
but while running the game I get the following:
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 ()
I am quite a novice and I do not understand very well what the error is, I hope you can help me, thanks.
Your answer
![](https://koobas.hobune.stream/wayback/20220613060159im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How do I modify variables from a different class that belongs to the same C# file? 1 Answer
How to add Script1.List.Class to Script2.List.Class? 0 Answers
Class with a List of Classes containing a List of Classes 1 Answer
NullReferenceException: Object reference not set to an instance of an object 0 Answers
NullReferenceException : Object reference not set to an instance of an object? 0 Answers