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 Oct 20, 2020 at 11:00 PM by umtklnc for the following reason:

The question is answered, right answer was accepted

avatar image
0
Question by umtklnc · Oct 19, 2020 at 09:25 PM · instantiateobjectlocationout of range

Index out of range when trying to spawn objects using spawn location list

Hello everyone, I'm new to Unity and C# I require a detailed help.

I'm currently creating my first self project where 3 platforms that the player shouldn't go and 1 platform that s/he should. These platforms are created around the player on specific positions (up, down, left, right). So I try to spawn these platforms on 4 random locations using a spawner location list. My aim is to create 3 false platforms on random locations without overlapping each other, and creating 1 true platform overlapping no other platform. My code to spawn platforms are in Spawner() function. But if I try to run my game, i get an error saying my randomSpawnerIndex is out of range on falsePlatform2:

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 using UnityEngine.SceneManagement;
 
 public class GameManager : MonoBehaviour
 {
     public PlayerController movement;
 
     public Transform player;
     public Transform playerPlatform;
     public Transform cameraPosition;
 
     public GameObject truePlatform;
     public GameObject falsePlatform;
     public GameObject Spawn1;
     public GameObject Spawn2;
     public GameObject Spawn3;
     public GameObject Spawn4;
 
     private Vector3 platformYOffset;
     private Vector3 cameraYOffset;
 
     bool gameHasEnded = false;
     public float restartDelay = 1f;
 
     void Start()
     {
         Spawner();
     }
 
 
     
 
     public void EndGame()
     {
         if(gameHasEnded == false)
         {
             gameHasEnded = true;
             Invoke("Restart", restartDelay);
         }
     }
 
     
     //RESTART
     
     void Restart()
     {
         SceneManager.LoadScene(SceneManager.GetActiveScene().name);
     }
 
 
     
 
 
     public void OnTrueMove()
     {
         platformYOffset = new Vector3(0, (float)0.5, 0);
         GameObject[] falsePlatforms = GameObject.FindGameObjectsWithTag("FalsePlatform");
         foreach (GameObject falseOne in falsePlatforms)
         {
             Destroy(falseOne);
         }
 
         truePlatform.GetComponent<Transform>().position = new Vector3(0, -3, 0);
 
         platformYOffset = new Vector3(0, (float)0.5, 0);
         cameraYOffset = new Vector3(0, 8, 0);
 
         playerPlatform.position = player.position - platformYOffset;
         cameraPosition.position = player.position + cameraYOffset;
         Spawner();
     }
 
     
     void Spawner()
     {
         List<Vector3> spawnLocs = new List<Vector3>();
         spawnLocs.Add(Spawn1.transform.position);
         spawnLocs.Add(Spawn2.transform.position);
         spawnLocs.Add(Spawn3.transform.position);
         spawnLocs.Add(Spawn4.transform.position);
 
         Debug.Log(spawnLocs.Count);
 
         platformYOffset = new Vector3(0, (float)0.5, 0);
 
         int randomSpawnerIndex = Random.Range(0, spawnLocs.Count);
 
         GameObject instantiatedFalse1 = Instantiate(falsePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
         spawnLocs.RemoveAt(randomSpawnerIndex);
         Debug.Log(spawnLocs.Count);
         GameObject instantiatedFalse2 = Instantiate(falsePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
         spawnLocs.RemoveAt(randomSpawnerIndex);
         Debug.Log(spawnLocs.Count);
         GameObject instantiatedFalse3 = Instantiate(falsePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
         spawnLocs.RemoveAt(randomSpawnerIndex);
         Debug.Log(spawnLocs.Count);
 
         GameObject instantiatedTrue = Instantiate(truePlatform, spawnLocs[randomSpawnerIndex] - platformYOffset, Quaternion.identity);
         spawnLocs.RemoveAt(randomSpawnerIndex);
     }
 }
 

I tried to create the true platform using the last element of the spawnLocs list.

I look forward to your help and any suggestions that make my very basic game mechanic become real.

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 umtklnc · Oct 20, 2020 at 10:00 AM 0
Share

I forgot to mention that the code works without any problem only on the below placements. But other than that; it creates 1, 2 or 3 red platforms but no green platform.

alt text

ekran-goruntusu-31.png (132.2 kB)

1 Reply

  • Sort: 
avatar image
0
Best Answer

Answer by Hellium · Oct 20, 2020 at 09:48 AM

You need to update randomSpawnerIndex after removing elements from the spawnLocs list.

I highly advise you to put the spawn logic in a function

 void Spawner()
 {
     List<Vector3> spawnLocs = new List<Vector3>();
     spawnLocs.Add(Spawn1.transform.position);
     spawnLocs.Add(Spawn2.transform.position);
     spawnLocs.Add(Spawn3.transform.position);
     spawnLocs.Add(Spawn4.transform.position);

     platformYOffset = new Vector3(0, 0.5f, 0);
     SpawnPlatform(spawnLocs, falsePlatform, platformYOffset);
     SpawnPlatform(spawnLocs, falsePlatform, platformYOffset);
     SpawnPlatform(spawnLocs, falsePlatform, platformYOffset);
     SpawnPlatform(spawnLocs, truePlatform, platformYOffset);
 }
 
 void SpawnPlatform(List<Vector3> spawnLocs, GameObject prefab, Vector3 offset)
 {
     if(spawnLocs.Count == 0)
     {
         Debug.LogError("No spawn location available");
         return;
     }
     int randomSpawnerIndex = Random.Range(0, spawnLocs.Count);

     GameObject instance = Instantiate(prefab, spawnLocs[randomSpawnerIndex] - offset, Quaternion.identity);
     spawnLocs.RemoveAt(randomSpawnerIndex);
 }
Comment
Add comment · Show 1 · 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 umtklnc · Oct 20, 2020 at 11:15 AM 0
Share

Thanks, much appreciated. The code works without an error and the game continues as I make the right moves.

Follow this Question

Answers Answers and Comments

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

[Closed]Instantiate object to spesific location 1 Answer

Instantiate a random prefab at an objects location 3 Answers

Spawning Objects at location 3 Answers

Instantiate an object every x meters between two others. 1 Answer

hello ! I want to instantiate a line on x axis. i have written the following code. i want to instantiate a line on x axis on touch please help 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