Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

Question Instantiate multiple items at multiple locations

Discussion in 'Scripting' started by key-es-co, Nov 21, 2022.

  1. key-es-co

    key-es-co

    Joined:
    Nov 5, 2022
    Posts:
    5
    Hi, my issue is the following:

    i have a game where i have 5 sets of four doors. Each set of 4 doors has 3 of the doors exactly the same and one of them is slightly different and the odd one out. the odd one out is the unlocked door and the other 3 locked. You need to pick the correct door to advance to the next set of four doors.

    I don't want the unlocked door to be in the same place every time so i want them to randomise the order.

    So far i'm working the issues with just one set of doors. I have a spawn manager with the script attached, 4 spawn points and 4 door prefabs (3 of the same, 1 Different).

    my script is as follows:

    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class DoorTest2 : MonoBehaviour
    6. {
    7.     public List<GameObject> spawnPositions;
    8.     public List<GameObject> spawnDoors;
    9.  
    10.     void Start()
    11.     {
    12.         SpawnDoors();
    13.     }
    14.  
    15.     void SpawnDoors()
    16.     {
    17.         foreach (GameObject spawnPosition in spawnPositions)
    18.         {
    19.             int selection = Random.Range(0, spawnDoors.Count);
    20.             Instantiate(spawnDoors[selection], spawnPosition.transform.position, spawnPosition.transform.rotation);
    21.         }
    22.     }
    23. }
    this works as in it will spawn in 4 doors in random locations, but it will just choose any 4 doors from the 4 in the list even if it duplicates them (I could have four locked doors or say 3 unlocked and 1 locked etc). I believe i need to delete a prefab from the list once its instantiated but not sure the best way to go about this.
     
  2. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Shuffle the prefabs list, then just iterate:
    Code (CSharp):
    1.     void SpawnDoors()
    2.     {
    3.         ShuffleList(spawnDoors);
    4.         for (int i = 0; i < spawnDoors.Length; i++)
    5.         {
    6.             instantiate(spawnDoors[i], spawnPositions[i].transform.position, spawnPositions[i].transform.rotation);
    7.         }
    8.     }
    As for the shuffle implementation: https://www.dotnetperls.com/fisher-yates-shuffle

    Another approach would be to just use two different prefab references instead of a parallel list and simply pick a random spot to place the special one:
    Code (CSharp):
    1.  
    2. public GameObject unlockedDoor;
    3. public GameObject lockedDoor;
    4. void SpawnDoors()
    5. {
    6.     int locked = Random.Range(0, spawnPositions.Length);
    7.     for (int i = 0; i < spawnPositions.Length; i++)
    8.     {
    9.         instantiate(i == locked ? lockedDoor : unlockedDoor, spawnPositions[i].transform.position, spawnPositions[i].transform.rotation);
    10.     }
    11. }
    12.  
     
    Last edited: Nov 21, 2022
  3. key-es-co

    key-es-co

    Joined:
    Nov 5, 2022
    Posts:
    5
    Hey, thanks so much for your responses. i have looked into both options and both of them are throwing up an error on the ".Length".

    List<GameObject> does not contain a definition for 'Length'.
    ive tried to add this to the start fuction to give a number to my List and it didnt work.

    Code (CSharp):
    1.         int listLength = spawnDoors.Count;
    i also tried to turn my list into an Array to counteract this but it doesn't like that either.
    i dont get why this is causing me such a headache as it should be a simple implementation.
     
  4. Strafe_m

    Strafe_m

    Joined:
    Oct 24, 2022
    Posts:
    72
    public Transform[] spawnPositions;
     
  5. PraetorBlue

    PraetorBlue

    Joined:
    Dec 13, 2012
    Posts:
    7,735
    Lists do not have a
    Length
    they have a
    Count
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    36,958
    Then FIX the error... it's the barest minimum of what we expect programmers to do!!

    Here's how:

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.