Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by Captain_Dando · Feb 22, 2013 at 11:45 AM · c#objectclass

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>'
Comment
Add comment · Show 1
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gheeler · Feb 22, 2013 at 11:51 AM 1
Share

List(<)Dialogue(>) boxList = new List(<)Dialogue(>)();

i had to put the round brackets because it was escaping the pointed ones

4 Replies

· Add your reply
  • Sort: 
avatar image
2
Best Answer

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:

  1. Place Dialog class before ArrayTest.

  2. Remove the inheritance from MonoBehaviour of Dialog.

  3. Change line to: List < Dialog > boxList = new List < Dialog >();

(Remove the unneeded spaces...they are there since otherwise the '' are regarded as HTML tags.

Comment
Add comment · Show 15 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image gheeler · Feb 22, 2013 at 11:52 AM 1
Share

he is not initializing a mono behaviour class. List is not a mono behaviour

avatar image GuyTidhar · Feb 22, 2013 at 11:53 AM 2
Share

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#)

avatar image gheeler · Feb 22, 2013 at 11:58 AM 1
Share

Dialogue doesnt need to be a monobehaviour, just remove that

avatar image GuyTidhar · Feb 22, 2013 at 12:04 PM 1
Share

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.

avatar image GuyTidhar · Feb 22, 2013 at 12:07 PM 1
Share

To sum up:

  1. Place Dialog class before ArrayTest.

  2. Remove the inheritance from $$anonymous$$onoBehaviour of Dialog.

  3. 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).

Show more comments
avatar image
2

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.

Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image paulaceccon · Feb 22, 2013 at 11:58 AM 0
Share

Oh my God, the doesn't appear in my answer. Where I put X, you have to put it.

avatar image gheeler · Feb 22, 2013 at 11:59 AM 1
Share

its being pulled out as html or something i think t(<)Dialogue(>)

avatar image
1

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by PAHeartBeat · Feb 22, 2013 at 12:53 PM

  1. Dialogue is MonoBehaviour class

  2. you can't use constructor for it dircetly so remove MonoBehaviour from first class

  3. 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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

13 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Distribute terrain in zones 3 Answers

Get GameObject transform inside a custom class 0 Answers

Serialization - Variables won't change on original construction 1 Answer

Instantiating objects from a class? (C#) 1 Answer

Creating instance of class that does not extend MonoBehaviour 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges