- Home /
Trouble with an array of classes - Cannot implicitly convert type
Hi everyone, I have created a script that I use to create and add to an array of objects. The code for the class constructor is as follows;
using UnityEngine; using System.Collections;
public class Dialogue: MonoBehaviour{
private int _x;
private int _y;
private string _text = "";
public int X {
get { return _x; }
set { _x = value; }
}
public int Y {
get { return _y; }
set { _y = value; }
}
public string Text {
get { return _text; }
set { _text = value; }
}
}
extending this, I have created a function in a separate file which enabled me to create new objects and add them to the array;
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class ArrayTest: MonoBehaviour {
List<Dialogue> boxList = new ArrayList();
public void DialogueAdd(string text, int width, int height){
Dialogue newDiag = new Dialogue();
newDiag.Dialogue = text;
newDiag.X = width;
newDiag.Y = height;
boxList.Add (newDiag);
}
void Start(){
DialogueAdd ("some text", 22, 42);
DialogueAdd ("some more text", 12, 24);
DialogueAdd ("even more text", 6, 22);
Debug.Log (boxList);
}
}
The problem is, when my script is compiled I get the following error;
Assets/Scripts/Dialogue/ArrayTest.cs(7,49): error CS0029: Cannot implicitly convert type `System.Collections.ArrayList' to `System.Collections.Generic.List<Dialogue>'
List(<)Dialogue(>) boxList = new List(<)Dialogue(>)();
i had to put the round brackets because it was escaping the pointed ones
Answer by GuyTidhar · Feb 22, 2013 at 11:51 AM
You can not use the 'new' keyword to initialize MonoBehaviour classes.
Edit: To sum up:
Place Dialog class before ArrayTest.
Remove the inheritance from MonoBehaviour of Dialog.
Change line to:
List < Dialog > boxList = new List < Dialog >();
(Remove the unneeded spaces...they are there since otherwise the '' are regarded as HTML tags.
he is not initializing a mono behaviour class. List is not a mono behaviour
Yes he does:
Dialogue newDiag = new Dialogue();
Dialog inherits $$anonymous$$onoBehvaiour.
(And why on earth would List have anything to do with $$anonymous$$onoBehaviour... it's a generic class of C#)
Dialogue doesnt need to be a monobehaviour, just remove that
Try putting the non monobehvaiour Dialog definition at the same file as ArrayTest, just before the ArrayTest class.
When the class is not a $$anonymous$$onoBehaviour, you can define it within an existing $$anonymous$$onoBehaviour class file.
To sum up:
Place Dialog class before ArrayTest.
Remove the inheritance from $$anonymous$$onoBehaviour of Dialog.
Change line to:
List < Dialog > boxList = new List < Dialog >();
(Remove the unneeded spaces...they are there since otherwise the '' are regarded as HT$$anonymous$$L tags).
Answer by paulaceccon · Feb 22, 2013 at 11:57 AM
You defined a List but you did a new ArrayList:
List< X >boxList = new ArrayList();
So:
List< X >boxList = new List< X >();
Also, your Dialogue class doesn't have to extend MonoBehavior.
Oh my God, the doesn't appear in my answer. Where I put X, you have to put it.
its being pulled out as html or something i think t(<)Dialogue(>)
Answer by gheeler · Feb 22, 2013 at 12:07 PM
that should read
List<Dialogue> boxList = new List<Dialogue>();
its escaping the after each list
Answer by PAHeartBeat · Feb 22, 2013 at 12:53 PM
Dialogue is MonoBehaviour class
you can't use constructor for it dircetly so remove MonoBehaviour from first class
you need to create constructor overload for the class Dialouge
public class Dialogue {
private int _x; private int _y; private string _text = ""; public int X { get { return _x; } set { _x = value; } } public int Y { get { return _y; } set { _y = value; } } public string Text { get { return _text; } set { _text = value; } } public Dialogue (string s, int x, int y) { _text = s; _x = x; _y = y; } }
now you can use it in List or arrayList