• 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
Question by Jarlarko · Feb 24, 2014 at 10:10 PM · gameobjectlistarraysaddarraylist

Problems creating new List of type GameObject and adding GameObject to it.

Hey,

I'm trying to create a new List (the shipList) whenever the shipPlaced function goes to 'else', and then placing 'currentShip' into it. The object gridShip refers to a GameObject in another script (which is sent over when the other script refers to shipPlaced. The other script would be various instances of 'GridSquare'). gridShip cannot be null when the method is called. x and y are the co-ordinates on a grid, and refer to the individual spot the method is called from. The error I am receiving in full is:

Assets/Scripts/ArrayHolder.cs(34,45): error CS0118: ArrayHolder.shipList' is a field' but a `type' was expected.

Should I be using arraylists or arrays instead? Or is there another problem? I would like to be able to dynamically create new Lists as well as change the length of them. Additionally, I would like to have a way of finding the List an object is in from only having the object itself. Is this possible?

Thanks in advance.

 using UnityEngine;
 using System.Collections.Generic;
 
 public class ArrayHolder : MonoBehaviour {
     
     GameObject[] gridArray = new GameObject[64];
     List<GameObject> shipList = new List<GameObject>();
     
 
     void Start ()
     {
         //need to create and add grid spots to gridArray
     }
     
     void Update () {
     
     }
     
     public void shipPlaced(int x, int y, GameObject gridShip)
     {
         foreach(GameObject item in gridArray)
         {
             if(x == x+1 && y == y || x == x-1 && y == y || x == x && y == y+1 || x == x && y == y-1)
             {
                 if(item.GetComponent<GridSquare>().currentShip == gridShip)
                 {
                     
                 }
                 
                 else
                 {
                     new shipList.Add(gridShip);//creates new ship list and adds ship to it
                 }
             }
         }            
     }
 }
 
Comment

People who like this

0 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 SAE_Superman · Feb 24, 2014 at 10:17 PM 0
Share

Remove the 'new' from this line:

 new shipList.Add(gridShip);//creates new ship list and adds ship to it

1 Reply

  • Sort: 
avatar image
Best Answer

Answer by Jamora · Feb 24, 2014 at 10:17 PM

What you're trying to do is create a new field, like the error says. What you need to be doing is create a new list, then add a gameobject to that.

So, instead of your line 32, you need to

 shipList = new List<GameObject>();
 shipList.Add(gridShip);

Alternatively, instead of creating a new list, you could also just empty the existing list with shipList.Clear();

Comment

People who like this

0 Show 5 · 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 SAE_Superman · Feb 24, 2014 at 10:20 PM 0
Share

He does not need the

 shipList = new List<GameObject>();

as he already does that on line 7.

avatar image Jamora · Feb 24, 2014 at 10:21 PM 0
Share

The OP clearly wants to clear the list. Creating a new List will have that effect.

If the list is not intended to be cleared, then removing the new - like you suggested - will do the trick.

avatar image Jarlarko · Feb 24, 2014 at 11:14 PM 0
Share

I would like to have multiple instances of the List,to store lots or groups of ships, but im not sure thats possible with lists. whats the most reliable alternative?

avatar image Jamora · Feb 25, 2014 at 08:39 AM 0
Share

I just can shake this feeling that there is a better way to achieve what is tried to be done here. Have you read this page?

Regardless, having a list of lists is possible: you declare it by

 List<List<GameObject>> allShipLists = new List<List<GameObject>>();

Now, adding to, e.g. the first list is achieved by

 allShipLists[0].Add(gridship);

You also mention you want to find which list a specific GameObject belings to. That is done by iterating through all element with for-loops:

 for(int i=0;i<allShipLists.Count;i++){
     for(int j=0;j<allShipLists[i].Count;j++){
         //check for equality here
     }
 }

You might also consider using Dictionaries. Using Dictionaries allows you to 'name' your lists, which you then access by this 'name'.

 Dictionary<string, List<GameObject>> allShipLists = new Dictionary<string, List<GameObject>>();
 
 //you then add a new list, named Group1 by
 allShipLists.Add("Group1",new List<GameObject>());
 
 //you add the gridShip to Group1 by
 allShipLists["Group1"].Add(gridShip);


avatar image Jarlarko · Feb 26, 2014 at 10:02 PM 0
Share

Thanks a lot Jamora, the information and links you provided where most helpful.

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta on June 13. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

22 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

Related Questions

Keep adding targets to a list 2 Answers

How to add and access objects in array? 1 Answer

GetComponent for gameobject list alternative 1 Answer

Filling up an array with GameObjects 0 Answers

GameObject to Array/List or whatever fits best 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