- Home /
How to instantiate a list of game objects multiple times?
I have a method which creates 2d sprites with specific width. After instantiate I add them to the list. I want to have multiple instances of this list. So I am planning to create two extra scripts. One to be responsible for positioning and order of these 2d sprites. (List basically serves as a group) and second for holding all variables.
How to instantiate sprite lists from another script using variables from the third?
I hope it make sense
Thank you very much
Answer by SiniKettu_ · Jun 19, 2020 at 08:49 PM
So you want to create in the scene, at runtime, several sprite prefabs, several times ?
If this is the case, you can use a foreach
. If you don't know how to use it : https://docs.microsoft.com/en-ie/dotnet/csharp/language-reference/keywords/foreach-in.
So you can create a bit of code wich, will instantiate every Sprite of your list (you can even set up some "filters" or selection criterias if needed ) and of course you can repeat this foreach
with another foreach
or with a for
If this is exactly what you need, and if you struggle with writing your code I can help you
Answer by Artpen · Jun 19, 2020 at 10:20 PM
Thank you. I have a script which creates road as a list from2d sprites. I will also have pavement script which does almost the same.
I want to put all variable for crating these into separate script Specification and create another script which handles positioning of road and pavement.
This is road class
public class Road : MonoBehaviour
{
[SerializeField]
private StreetSpec newlaneWidth;
//Road control variables
public bool isRoadOnBothSides;
public GameObject lanePrefab;
public float laneWidth;
public int numberOfLanes;
private List<GameObject> laneList = new List<GameObject>();
private float laneOffset;
//Creates Road form lane objects.
public List<GameObject> MakeRoad()
{
if (laneList.Count < numberOfLanes)
{
AddLane();
}
else if (laneList.Count > numberOfLanes)
{
RemoveLane();
}
ScaleLane(); //scales lane gameobjects in run time
CheckLanePosition(); // checks and adjust lane position after scale change
return laneList;
}
//Adds lane
public void AddLane()
{
laneOffset = laneWidth * laneList.Count;
GameObject lane = Instantiate(lanePrefab, new Vector3(laneOffset, 0, 0), Quaternion.identity);
laneList.Add(lane);
}
//Scales lane
public void ScaleLane()
{
for (int i = 0; i < laneList.Count; i++)
{
laneList[i].transform.localScale = new Vector3(laneWidth, 1, 1);
}
}
//Checks lane position
public void CheckLanePosition()
{
for (int i = 0; i < laneList.Count; i++)
{
laneList[i].transform.position = new Vector3(laneWidth * i, 0, 0);
}
}
//Removes lane
public void RemoveLane()
{
GameObject destroyLane = laneList[laneList.Count - 1];
laneList.Remove(destroyLane);
Destroy(destroyLane);
}
}
And this is generator class:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class StreetGenerator : MonoBehaviour
{
Road road; // reference to Road class
// Start is called before the first frame update
void Start()
{
road = FindObjectOfType<Road>(); // find Road class in the scene
}
// Update is called once per frame
void Update()
{
road.MakeRoad();
}
}
I saw a lot of tutorials about managers and player controllers. But they are very specific to games. I am trying to have a set of classes which make staff, a class to hold all variables (so I can use ui to control it) and a class to instantiate all different game objects on the screen in a correct order, size an number using information from variable class.
A very small example will help me understand how to make it will be a great help
Thank you
I'm afraid i can't understand what you need (a versy small example of what ?) (it's probably my fault, I don't speak English very well)
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Dictionary Keys to Stack Enemy Types 1 Answer
Distribute terrain in zones 3 Answers
Fastest, cleanest way to find common items in 2 lists 2 Answers
Can't move instantiated 2d game oject. 0 Answers