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 MountainDrew · Aug 27, 2015 at 01:04 AM · listlists

How to get position of an object in a list

I'm new to Unity and C# (and OOP in general) and I'm having difficulty working with lists.

In the following code I'm successfully instantiating new objects and adding them to the list. Then when I try to output the position to Debug.Log I get an error: "Object reference not set to an instance of an object". It compiles and runs fine until it gets to the Debug.Log line. How can I grab a specific instance of an object from a list by that object's index from within the list?

Thank you!

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;
 
 public class GameController : MonoBehaviour {
 
     public GameObject unitToSpawn;
     private List<Tank> TankList = new List<Tank>();
     public Tank tank;
     public int numTanks;
 
     void Start () {
 
         for (int i = 0; i < numTanks; i++) {
             Vector3 spawnPosition = new Vector3 (Random.Range (-5, 5), 1, Random.Range (-5, 5));
             Quaternion spawnRotation = Quaternion.identity;
             TankList.Add (Instantiate (unitToSpawn, spawnPosition, spawnRotation) as Tank);
         }
         for (int i = 0; i < numTanks; i++){
             Debug.Log ("i = " + i + " : " + TankList[i].transform.position);
         }
     }
 }

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

2 Replies

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

Answer by MountainDrew · Aug 29, 2015 at 12:09 AM

So I ended up doing something similar to the way a list of Enemy objects is made in the 2D Roguelike Tutorial. In case anyone else comes across this problem here is the solution I got to:

GameController.cs:

 using UnityEngine;
 using System.Collections;
 using System.Collections.Generic;

 public class GameController : MonoBehaviour {

     public static GameController instance = null;
     public GameObject tankPreFab;
     public int numTanks;
     private List<Tank> tankList;

     void Awake (){
         instance = this;
         tankList = new List<Tank>();
     }

     void Start () {
         for (int i = 0; i < numTanks; i++) {
             Vector3 spawnPosition = new Vector3 (Random.Range (-5, 5), 1, Random.Range (-5, 5));
             Quaternion spawnRotation = Quaternion.identity;
             Instantiate(tankPreFab, spawnPosition, spawnRotation);
         }
         for (int i = 0; i < numTanks; i++){
             tankList[i].ID = i;
             Debug.Log ("i = " + i + " : " + tankList[i].transform.position);
         }
     }

     public void AddTankToList(Tank script)
     {
         tankList.Add(script);
     }
 }

And here is Tank.cs (which is attached to the Tank Prefab)

 using UnityEngine;
 using System.Collections;

 public class Tank : MonoBehaviour {
     public int ID;    //unique ID for each tank
     void Awake(){
         GameController.instance.AddTankToList (this);
     }
 }

And now I have a list of tanks!

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
1

Answer by Vice_Versa · Aug 27, 2015 at 07:48 AM

this is not a problem with Getting the position in the list, you are doing that part correctly. Your not setting a value to Tank in the Start function, So when you make these new objects as type Tank, your giving them unknown values. instead of doing it "as Tank" do it "as GameObject" or "as Transform" and it should work

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 MountainDrew · Aug 27, 2015 at 03:11 PM 0
Share

Thanks for your reply. So it works if I change the List declaration to

 private List<GameObject> TankList = new List<GameObject>();

and then do set as GameObjectbut now my list is a list of GameObjects ins$$anonymous$$d of a list of Tanks. Is there a way to do it as a list of tanks? Here is my Tank.cs file:

 using UnityEngine;
 using System.Collections;
 public class Tank : $$anonymous$$onoBehaviour {
 public int ID;    //unique ID for each tank
 }

Is there something I can do so that I can have a list of objects of the Tank class? I was hoping that because Tank inherits from $$anonymous$$onoBehaviour that it would also have Transform and all that good stuff. Again, I'm new to OOP so I could be misunderstanding how to appropriately use classes.

Thanks!

avatar image Vice_Versa · Aug 27, 2015 at 04:28 PM 0
Share

you need to make a seperate script called Tank, and give it whatever properties Tank needs, then at the top of this script do "using Tank;"

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

27 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

Related Questions

How to know if a list contains GameObjects with a specific Varaible values ? 0 Answers

How do I modify variables from a different class that belongs to the same C# file? 1 Answer

List resulting in out of range 0 Answers

Find the closest Enemy that has a tag added in a list? 1 Answer

Inventory slots number are wrong 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