• 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 hijinxbassist · Jun 15, 2012 at 06:15 AM · c#intbool

Add bool to Object List

I have a jagged list which contains(or should contain) multiple value types, GameObject,int,bool...and whatever else i might throw in there. Im having a tough time with the C# version of the script as i am a noob with this language. The UnityScript version works perfectly, but my conversion has been going slow thanks to this current problem.

error CS1503: Argument #1' cannot convert bool' expression to type UnityEngine.Object' error CS1503: Argument #1' cannot convert int' expression to type UnityEngine.Object'

To my understanding i should be able to add an int and bool to an Object List, maybe theres a special way to do it in C#?? Heres my Jagged List

 var jaggedList = new List<List<Object> >();
 
 foreach(var ship in ships)
 {
     jaggedList.Add( new List<Object>() );
 }
 
 for(int i=0;i<jaggedList.Count;i++)
 {
     var sh=ships[i];
     var aiPrimary=sh.GetComponent<AI_Primary>();
     jaggedList[i].Add(sh);                  // GO
     jaggedList[i].Add(aiPrimary.friendly);  // bool
     jaggedList[i].Add(aiPrimary.unitID);    // int
 }

Thanks in advance!

Comment
Add comment
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

1 Reply

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

Answer by flamy · Jun 15, 2012 at 06:29 AM

Idk how to add bool to the list but in these cases i usually use a custom class like this

 public class Enemy
 {
 
  public GameObject go;
  public bool isFriendly;
  public int id;
 }

 and create a list of type Enemy.  
 List<Enemy> jaggedList;

and add the object of _enemy class instead of adding individual objects. Anyway from ur code what i understand is you are not exactly using a jagged list, it is just a nested one!! so i hope this would be a good approach..

 for(int i=0;i<jaggedList.Count;i++)
 {
     var sh=ships[i];
     var aiPrimary=sh.GetComponent<AI_Primary>();
     Enemy _enemy = new Enemy();
     _enemy.go=sh;
     _enemy.isFriendly=aiPrimary.friendly;
     _enemy.id=aiPrimary.unitID;
     jaggedList.Add(_enemy);
 
 }

Im not sure if this is wat you are looking for!!

if it is really a jagged list that hold various data types of various length, you have to be declaring the list as System.Object, instead of UnityEngine.Object or just object.

Comment
Add comment · Show 6 · 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 hijinxbassist · Jun 15, 2012 at 06:55 AM 0
Share

Im not sure what a nested list is, but im pretty sure what im using is a jagged list. for each ship i add a list() into my jagged list which is defined as

 var jaggedList = new List<List<Object> >();

 foreach(var ship in ships)
 {
     jaggedList.Add( new List<Object>() );
 }
avatar image flamy · Jun 15, 2012 at 06:59 AM 0
Share

oh my bad i understood it wrong... actually this case is simple you have to declare it as

var jaggedList = new List >();

foreach(var ship in ships) { jaggedList.Add( new List() ); }

System.Object 's object can store every datatype in c#

avatar image flamy · Jun 15, 2012 at 07:01 AM 0
Share

with just the for loop in the question, i thought it is just fixed to 3 datatype ..

avatar image hijinxbassist · Jun 15, 2012 at 07:06 AM 0
Share

$$anonymous$$y bad, wanna add that answer to a new answer delete this one, so i can select that as being correct. I appreciate the help, i was $$anonymous$$ring my hair out trying to find the solution ;) BTW worked like a charm!

avatar image hijinxbassist · Jun 15, 2012 at 07:10 AM 1
Share

Oh ok, awesome! Thanks again!

Show more comments

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Multiple Cars not working 1 Answer

Distribute terrain in zones 3 Answers

buttons don't work if I use int 1 Answer

bool[string] = true; array possible? 2 Answers

Can't add multiple integers to a List in a Loop! 2 Answers


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