Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Question Creating a script to instantiate entities from one list to a group of locations on another list

Discussion in 'Scripting' started by wdessy, May 23, 2024.

  1. wdessy

    wdessy

    Joined:
    Nov 17, 2023
    Posts:
    5
    Hello!

    I have a list of 5 AI entities and 5 structures, in my game. List of entities can be "LIST A" and list of structures can be "LIST B". I want to write a script that, at the beginning of each round, takes an entity from LIST A and randomly instantiates it into a structure from LIST B, so that each structure will contain ONE entity at the beginning of each round.

    I need to make sure that once an entity from LIST A and a structure from LIST B are matched up, neither of them can be matched again for that round.

    Any assistance with this would be greatly appreciated! Thank you.
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    Get to it! Scripts are not written on the internet, they are written and debugged on your computer.

    This is an excellent way to get started:

    Imphenzia: How Did I Learn To Make Games:



    Stop making excuses!



    The purpose of this forum is to assist people who are ready to learn by doing, and who are unafraid to get their hands dirty learning how to code, particularly in the context of Unity3D.

    This assumes you have at least written and studied some code and have run into some kind of issue.

    If you haven't even started yet, go check out some Youtube videos for whatever game design you have in mind. There are already many examples of the individual parts and concepts involved, as there is nothing truly new under the sun.

    If you just want someone to do it for you, you need go to one of these places:

    https://forum.unity.com/forums/commercial-job-offering.49/

    https://forum.unity.com/forums/non-commercial-collaboration.17/

    https://livehelp.unity.com/?keywords=&page=1&searchTypes=lessons

    Remember this is YOUR game, not ours. We have our own games we're working on.
     
  3. wdessy

    wdessy

    Joined:
    Nov 17, 2023
    Posts:
    5
    I apologize if I gave the impression that I haven't been beating my head against a wall trying to figure it out. I've been researching for weeks and have written several scripts but nothing does it the way I need it to work. Sorry, I've taught myself how to code in unity from the ground up and didn't make that very clear.

    I have built an entire 3d game within unity. I've designed AI and built the entire environment. I simply needed a little help to figure out how to instantiate objects from one list into objects from another list, because I'm not finding anything within the forums.

    With that said, if it would help, I can share what I've done that hasn't worked and maybe get some help to build upon that. Or, if there are similar articles that I missed, that would helpful too. Thank you.
     
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    21,683
    The Scripting section typically sees some combination of beginner (primarily early beginner) and advanced topics but the more intermediate ones don't tend to be a thing. Here's a basic script. I've not tested it. I just typed it up in VSC outside of Unity with the help of the MSDN List docs.

    https://learn.microsoft.com/en-us/dotnet/api/system.collections.generic.list-1

    Code (csharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4.  
    5. public class Spawner : MonoBehaviour
    6. {
    7.     public List<GameObject> entityList;
    8.     public List<GameObject> structureList;
    9.  
    10.     public void Spawn()
    11.     {
    12.         if (entityList == null || entityList.Count == 0)
    13.         {
    14.             Debug.LogError($"Spawner has no assigned entities.", this);
    15.             return;
    16.         }
    17.    
    18.         if (structureList == null || structureList.Count == 0)
    19.         {
    20.             Debug.LogError($"Spawner has no assigned structures.", this);
    21.             return;
    22.         }
    23.    
    24.         var entitiesLeft = new List<GameObject>();
    25.         entityList.CopyTo(entitiesLeft);
    26.  
    27.         var structuresLeft = new List<GameObject>();
    28.         structureList.CopyTo(structuresLeft);
    29.  
    30.         while (entitiesLeft.Count > 0 && structuresLeft.Count > 0)
    31.         {
    32.             var entityIndex = Mathf.Round(0, entitiesLeft.Count);
    33.             var entity = entitiesLeft[entityIndex];
    34.             entitiesLeft.RemoveAt(entityIndex);
    35.  
    36.             var structureIndex = Mathf.Round(0, structuresLeft.Count);
    37.             var structure = structuresLeft[Mathf.Round(0, structuresLeft.Count)];
    38.             structuresLeft.RemoveAt(structureIndex);
    39.  
    40.             Instantiate(entity, structure.transform.position, Quaternion.Identity);
    41.         }
    42.     }
    43. }
     
    wdessy likes this.
  5. wdessy

    wdessy

    Joined:
    Nov 17, 2023
    Posts:
    5
    Ryiah,

    Thank you! I can already see some code that I haven't tried yet in your script example, that looks like it could be helpful. I am going to implement it and see how it turns out. I will report back.
     
  6. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357
    No problem... the key to any of this stuff is always to break it up into digestible pieces, even if it seems ridiculous, just like that guy in the video above.

    For instance, here's how you should go at your task, without EVER leaving a step until you understand 100% of it.

    - make a script that spawns one prefab
    - modify that script to spawn that thing five times in a row across the map
    - modify that script to include a public list of GameObjects (where you drag your prefab)
    - now learn how to iterate lists and iterate that list with the same variable you used to do five times in step 2
    - now instead of spawning the same thing, pull the prefab from the list you made and instantiate THAT
    - now repeat for the second set of things containing the position, setting the position as you instantiate.
     
    wdessy and Ryiah like this.
  7. wdessy

    wdessy

    Joined:
    Nov 17, 2023
    Posts:
    5
    Hello again!

    I've finally put together a script that works, almost perfectly. I took both of your advice and used the resources shared, so thank you both. However, for some reason, I cannot get the objects from the entityList to not repeat. What's happening is entities from entityList are being assigned to multiple structures. Ideally, what should happen is once an entity is assigned to a structure, it should be removed from the entityList for the next count. Here is my code:

    Code (CSharp):
    1. public class SpawnEntities : MonoBehaviour
    2. {
    3.     public EnterStructureTrigger[] m_EntityStructureTriggers;
    4.     public GameObject[] m_Entities;
    5.  
    6.     private void Start()
    7.     {
    8.         if (m_EntityStructureTriggers.Length != m_Entities.Length)
    9.         {
    10.             Debug.LogError("Error: The structure trigger array must be the same length as the entity array.");
    11.             return;
    12.         }
    13.  
    14.         AssignEntities();
    15.     }
    16.  
    17.     private void AssignEntities()
    18.     {
    19.         var entityList = new List<GameObject>(m_Entities);
    20.         var index = 0;
    21.         while (entityList.Count > 0)
    22.         {
    23.             var randomIndex = Random.Range(0, entityList.Count - 1);
    24.             m_EntityStructureTriggers[index].Entity = entityList[randomIndex];
    25.             entityList.RemoveAt(randomIndex);
    26.             index++;
    27.         }
    28.     }
    29. }
     
  8. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    39,357