Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by jeffreywarf · Feb 03, 2016 at 05:09 AM · c#list

Problem with List.Addrange

I was trying to create a dynamic array, apparently Unity does not like how I decided to type it. I tried it in a different script and it worked, but when I moved all the code to another script, Unity will not compile it.

So where is the problem and how can I resolve it in a way that allows me to do what I want?

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 using System.Diagnostics;
 
 public class CSHARPCONTROLLER : MonoBehaviour {
 
     public bool in_prompt = false;
     public bool object_selected = false;
     public List<string> object_names;
     public List<GameObject> all_objects;
 
     void Start () {
 
         object_names = new List<string> ();
         all_objects = new List<GameObject> ();
 
     }
 
     public void AddName (GameObject object_added, string name) {
         all_objects.AddRange (object_added);
         object_names.AddRange (name);
         UnityEngine.Debug.Log (name);
     }
 }
 

The error it gives me is this, and highlights exactly the lines where I try to use "AddRange".

 Assets/Standard Assets/Script/CSHARPCONTROLLER.cs(27,29): error CS1502: The best overloaded method match for `System.Collections.Generic.List<UnityEngine.GameObject>.AddRange(System.Collections.Generic.IEnumerable<UnityEngine.GameObject>)' has some invalid arguments

it also gives this error

 Assets/Standard Assets/Script/CSHARPCONTROLLER.cs(27,29): error CS1503: Argument `#1' cannot convert `UnityEngine.GameObject' expression to type `System.Collections.Generic.IEnumerable<UnityEngine.GameObject>'
 
Comment
Add comment · Show 2
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 jeffreywarf · Feb 03, 2016 at 07:41 AM 0
Share

For some strange reason it IS working as intended now, but I did not change anything except I stopped using "AddRange". Before when I stopped using it, it would not dynamically change the size of the List, but now it does? In any case, I still want to know what happened.

avatar image cjdev · Feb 03, 2016 at 09:14 AM 0
Share

AddRange has to add a collection of GameObjects but you only put in one.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by BenWiller1989 · Apr 13, 2021 at 10:12 PM

Accidentally stumbled over this. But for those, who have similar problems with that....


You can not add a single GameObject to AddRange() Statement! The same issue with the string. If you like to just Add a single Variable, then use List.Add(), not List.AddRange(), because AddRange expects a Range, not a single variable !

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

76 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there anyway to make a list of prewritten variables? (C#) 2 Answers

Insert string into empty list at a specific index 0 Answers

How to get all children of a Gameobject with a certain component 2 Answers

I need help finding the index of an object in a list. 1 Answer

Getting certain values from an item in a list 0 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