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 /
This question was closed Dec 11, 2014 at 07:04 PM by unimechanic for the following reason:

Duplicate Question

avatar image
0
Question by misterRobot · Dec 10, 2014 at 09:30 PM · transformpositionlistcoordinates

Finding Coordinates of Objects in a List

I made a list of Tiles that are instantiated. I want to retrieve a location of objects later in a new function. I tried using transform.position and GetComponent().transform.position but I keep getting NullReferenceException: Object reference not set to an instance of an object. This line is where the error occurs: Debug.Log ("Tiles: " + tiles[i].GetComponent().transform.position);

 public List<Tile> tiles = new List<Tile>();
 public int gridWidth;
 public int gridHeight;
 public GameObject tilePrefab;

 // Use this for initialization
 void Start () {
 
     for (int y = 0; y < gridHeight; y++) {
         for (int x = 0; x < gridWidth; x++)
         {
             GameObject g = Instantiate(tilePrefab, new Vector3(x,y,1), Quaternion.identity) as GameObject;
             g.transform.parent = gameObject.transform;
             tiles.Add(g.GetComponent<Tile>());
         }
     }

     gameObject.transform.position = new Vector3 (-3.5f, -3.5f, 1);
 }

 public void CheckTiles(){

         for (int i = 0; i < tiles.Count; i++) {    
             Debug.Log ("Tiles Count: " +tiles.Count);
             Debug.Log ("Tiles: " + tiles[i].GetComponent<Tile>().transform.position);
             Debug.Log("tiles : " + tiles[i].transform.position);
         }
 }
Comment
Add comment · Show 5
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 meat5000 ♦ · Dec 10, 2014 at 05:59 PM 1
Share

Paste the nullref

avatar image RudyTheDev · Dec 10, 2014 at 09:33 PM 0
Share

Please always post full errors and the exact line on which it happens. Presumably g.GetComponent< Tile >() is null. So presumably tilePrefab doesn't have a Tile script. Unless g in g.GetComponent< Tile >() is null, which means tilePrefab is null.

avatar image misterRobot · Dec 10, 2014 at 09:40 PM 0
Share

This line is where the error occurs: Debug.Log ("Tiles: " + tiles[i].GetComponent().transform.position);

avatar image Cherno · Dec 10, 2014 at 09:59 PM 0
Share

Since the list contains instances of the Tile class, it seems like trying to access the component Tile of the currently iterated tile won't work. It would work were the list composed of gameObjects, though. If you want to get the transform.position of the gameobject whose Tile component is included in the list, try something like

 Debug.Log("tiles : " + tiles[i].gameObject.transform.position);

which should work, but the way you did it in the second Debug line should be fine as well. So in short, the line tiles[i].GetComponent().transform.position makes no sense and rightly gives an error, so just delete it and use the second one or the one I posted above for the transform.position.

avatar image misterRobot · Dec 10, 2014 at 10:31 PM 0
Share

I tried using tiles[i].gameObject.transform.position) but the error is the same. NullReferenceException: Object reference not set to an instance of an object.

1 Reply

  • Sort: 
avatar image
0

Answer by unimechanic · Dec 11, 2014 at 07:03 PM

http://answers.unity3d.com/questions/131151/object-reference-not-set-to-instance-of-an-object.html

http://answers.unity3d.com/questions/512306/nullreferenceexception-object-reference-not-set-to-50.html

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

Follow this Question

Answers Answers and Comments

6 People are following this question.

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

Related Questions

A node in a childnode? 1 Answer

Transform vector to other coordinate frame using quaternions 1 Answer

Best way to find which two transforms in a list of transforms a point lies between. 0 Answers

Using custom Transform/GameObject classes? 0 Answers

How do I instantiate mutliple prefabs at the same location of a series of empty objects I put in the scene? 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