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 SteamPunkDevil_ · Apr 20, 2016 at 12:00 AM · c#instantiateif statementwhile-loop

Error Instantiating Prefab in while look at end of row, Happens after a random amount of rows

I am working on a simple world generator using squares. But is its failing after a random number of rows, It does not instanate the last item at the end

Error:

 ArgumentOutOfRangeException: Argument is out of range. Parameter name: index System.Collections.Generic.List`1[UnityEngine.GameObject].get_Item (Int32 index) (at /Users/builduser/buildslave/mono/build/mcs/class/corlib/System.Collections.Generic/List.cs:633) GenGround.Start () (at Assets/Code/GenGround.cs:93)

Code:

 using UnityEngine;
 using System.Collections;
 using System.Linq;
 //using System;
 using System.Collections.Generic;
 
 public class GenGround: MonoBehaviour {
  // Use this for initialization
  public GameObject[] terrainTypes;
  void Start() {
   int x = 0;
   int y = 0;
   int i = 0;
   GameObject[] itemList = new GameObject[10000];
   terrainTypes = GameObject.FindGameObjectsWithTag("Terrain").OrderBy(go => go.name).ToArray();
   List < GameObject > WaterDeepOpts = new List < GameObject > ();
   WaterDeepOpts.Add(terrainTypes[3]);
   WaterDeepOpts.Add(terrainTypes[4]);
   List < GameObject > WaterShallowOpts = new List < GameObject > ();
   WaterShallowOpts.Add(terrainTypes[3]);
   WaterShallowOpts.Add(terrainTypes[4]);
   WaterShallowOpts.Add(terrainTypes[1]);
   List < GameObject > SandOpts = new List < GameObject > ();
   SandOpts.Add(terrainTypes[0]);
   SandOpts.Add(terrainTypes[1]);
   SandOpts.Add(terrainTypes[4]);
   List < GameObject > GrassOpts = new List < GameObject > ();
   GrassOpts.Add(terrainTypes[0]);
   GrassOpts.Add(terrainTypes[1]);
   GrassOpts.Add(terrainTypes[2]);
   List < GameObject > StoneOpts = new List < GameObject > ();
   StoneOpts.Add(terrainTypes[0]);
   StoneOpts.Add(terrainTypes[2]);
   List < GameObject > Block1 = new List < GameObject > ();
   List < GameObject > Block2 = new List < GameObject > ();
   List < GameObject > Blocks = new List < GameObject > ();
   foreach(GameObject g in terrainTypes) {
    Debug.Log(g);
   };
   itemList[i] = Instantiate(terrainTypes[Random.Range(0, terrainTypes.Length)], new Vector3(0, 0, 0), Quaternion.identity) as GameObject;
   x = x + 10;
   i = i + 1;
   while (y < 1000) {
    if (i < 100) {
     //Debug.Log (i);
     if (itemList[i - 1].name == "WaterDeep(Clone)") {
      itemList[i] = Instantiate(WaterDeepOpts[Random.Range(0, WaterDeepOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
     } else if (itemList[i - 1].name == "WaterShallow(Clone)") {
      itemList[i] = Instantiate(WaterShallowOpts[Random.Range(0, WaterShallowOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
      //Debug.Log (itemList [i]);
     } else if (itemList[i - 1].name == "Sand(Clone)") {
 
      itemList[i] = Instantiate(SandOpts[Random.Range(0, SandOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
     } else if (itemList[i - 1].name == "Grass(Clone)") {
      itemList[i] = Instantiate(GrassOpts[Random.Range(0, GrassOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
     } else if (itemList[i - 1].name == "Stone(Clone)") {
      itemList[i] = Instantiate(StoneOpts[Random.Range(0, StoneOpts.Count())], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
     } else {
      itemList[i] = Instantiate(terrainTypes[Random.Range(0, terrainTypes.Length)], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
     }
    } else if (y > 0 && x > 0) {
     //Debug.Log(itemList[i - 1].name);
     //  Debug.Log(x);
     if (itemList[i - 1].name == "WaterDeep(Clone)") {
      Block1 = WaterDeepOpts;
     } else if (itemList[i - 1].name == "WaterShallow(Clone)") {
      Block1 = WaterShallowOpts;
     } else if (itemList[i - 1].name == "Sand(Clone)") {
      Block1 = SandOpts;
     } else if (itemList[i - 1].name == "Grass(Clone)") {
      Block1 = GrassOpts;
     } else {
      Block1 = StoneOpts;
     }
     if (itemList[i - 99].name == "WaterDeep(Clone)") {
      Block2 = WaterDeepOpts;
     } else if (itemList[i - 99].name == "WaterShallow(Clone)") {
      Block2 = WaterShallowOpts;
     } else if (itemList[i - 99].name == "Sand(Clone)") {
      Block2 = SandOpts;
     } else if (itemList[i - 99].name == "Grass(Clone)") {
      Block2 = GrassOpts;
     } else {
      Block2 = StoneOpts;
     }
     Blocks = Block1.Intersect(Block2).ToList();
     //Debug.Log(Blocks.Count());
     int d = Random.Range(0, Blocks.Count());
     //  Debug.Log(d);
     foreach(GameObject g in Blocks) {
      Debug.Log(g);
     };
     Debug.Log("Blocks = " + Blocks[d].name);
     itemList[i] = Instantiate(Blocks[d], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
    } else if (y > 0 && x == 0) {
     //  Debug.Log(x);
     if (itemList[i - 99].name == "WaterDeep(Clone)") {
      //  Debug.Log("BONG!");
      Blocks = WaterDeepOpts;
     } else if (itemList[i - 99].name == "WaterShallow(Clone)") {
      //  Debug.Log("BONG!");
      Blocks = WaterShallowOpts;
     } else if (itemList[i - 99].name == "Sand(Clone)") {
      //    Debug.Log("BONG!");
      Blocks = SandOpts;
     } else if (itemList[i - 99].name == "Grass(Clone)") {
      //    Debug.Log("BONG!");
      Blocks = GrassOpts;
     } else {
      Blocks = StoneOpts;
      //  Debug.Log("BONG!");
     }
     //Blocks = Block1.Intersect(Block2).ToList();
     //  Debug.Log("BONG!");
     int d = Random.Range(0, Blocks.Count());
     Debug.Log(d);
     Debug.Log("count=" + Blocks.Count());
     itemList[i] = Instantiate(Blocks[d], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
    } else {
     itemList[i] = Instantiate(terrainTypes[Random.Range(0, terrainTypes.Length)], new Vector3(x, 0, y), Quaternion.identity) as GameObject;
    }
    x = x + 10;
    i = i + 1;
    //Debug.Log(itemList[i]);
    if (x == 1000) {
     y = y + 10;
     x = 0;
 
    };
 
   };
 
  }
 
 
  // Update is called once per frame
  void Update() {
 
  }
 }
 


error.png (307.5 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

143 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 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

Add changeable conditions in the editor, similar to unityEvent? 0 Answers

When re-parenting instantiated object as child of current gameObject, why does GetComponents() not retrieve instantiated object's components? 1 Answer

scroll rect UI buttons all do the same thing 0 Answers

I got Some Error about how to Transform Postiton Enemy where one point can spawn 2 enemies. 1 Answer

How does one continuously move an object forward without using velocity? 1 Answer


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