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 RegularSage · Nov 03, 2015 at 12:30 AM · listfor-loop

Why is my for loop not instantiating object at correct possision from List

My code complies and runs however It is not working as intended. The desired outcome is for a "testTile " to be placed for every Vector3 in gridPosition. This is the result of the current code.

http://imgur.com/2BgvTLY

As you can see gridPositions contains 6 different Vector3's and 6 tiles where instantiated. There is no tile at (2,1,0) and instead an extra tile at one of the other positions. I suspect that the problem lies in the for loop but I'm out of ideas for what to try.

I have tried to add +1 or -1 to various parts of the for loop but I get an extra undesired tile and an "ArgumentOutOfRangeException" message. I thought it would be solved by adding a nullable to the end of the list but that can't be done (so far as I know). Just what is going on here, and is there a way to right this where I get all 6 tiles to match up with the correct vector3?

  public class TestLevelManager : MonoBehaviour
     {
         public class Grid
         { 
             public int sizeX;
             public int sizeY;
             public List<Vector3> gridLocations;
           
             public Grid(int x, int y, List<Vector3> list)
             {
                 sizeX = x;
                 sizeY = y;
                 gridLocations = list;
             }
     
             public void CreateGrid()
             {
                 for (int x = 0; x < sizeX; x++)
                 {
                     for (int y = 0; y < sizeY; y++)
                     {
                         Vector3 _position = new Vector3(x, y, 0f);
                         gridLocations.Add(_position);
                     }
                 }
             }
         }
     
         void SpawnTiles()
         {
             for (int i = 0; i < gridLocations.Count; i++)
             {
                 Instantiate(testTile[0]);
                 testTile[0].transform.position = (Vector3)gridLocations[(i)];
             }
         }
     
     
         private int rows, colums;
         public List<Vector3> gridLocations;
         public GameObject[] testTile;
     
     
         void Awake()
         {
             List<Vector3> gridLocations = new List<Vector3>();
         }
     
         void Start()
         {
             rows = Random.Range(1, 4);
             colums = Random.Range(1, 4);
             Grid cellGrid = new Grid(rows, colums, gridLocations);
             cellGrid.CreateGrid();
             SpawnTiles();    
         }
     }




Comment
Add comment · 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 ben-rasooli · Nov 03, 2015 at 02:03 AM 0
Share

I don't think the way you instantiate would work properly. When you instantiate, you need to assign(cast) it to a variable, then modify that object. Or in your case, you can pass the position as the second argument to the Instantiate() method. Have a look at the documentation: http://docs.unity3d.com/ScriptReference/Object.Instantiate.html

1 Reply

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

Answer by HenryStrattonFW · Nov 06, 2015 at 03:09 PM

The issue here is that you instantiate, then you move the object you cloned, not the clone itself, so the problem is for the last position is you clone, and then move the source not the copy.

@ben.rasooli has a good point, when you do your instantiate call, assign that to a GameObject local variable, and then use that game object to set the position from your array, that should do the job.

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How to prevent an overflow? 1 Answer

How to load multiple sprites at runtime from List? 1 Answer

List.contains doesn't work and add duplicates 1 Answer

List, for loop and removal of items 2 Answers

Gameobject list does not retrieve the first index in for loop 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