- Home /
Spawning from two different points with text
I want to spawn the obstacle exactly as per the below link.
https://www.youtube.com/watch?v=uQiwC06_cI8
I want exactly the same functionality of spawning obstacles with numbering. Two problems I faced: 1. I tried with two prefabs i.e. right/left with right Z position is 90 and left Z position is 60. But in this case in 02 seconds already 2 to 3 right obstacles shown on screen then left one will show 2. Unable to add increasing number functionality.
Anyone please guide me.
Answer by ogulcan_topsakal · Apr 17 at 05:15 PM
@faizanahmed089 You can set the correct increment values to the text by keeping the increment value in a variable and incrementing this value by one for each obstacle you spawn. I'm keeping my example very simple as I don't know at what level you are in software. Here is the example:
[SerializeField]
private GameObject obstaclePrefabRight;
[SerializeField]
private GameObject obstaclePrefabLeft;
private int _spawnedObjectCount;
public bool IsGameOver { get; set; } = false;
public void StartSpawner(float initialDelay, float interval)
{
StartCoroutine(StartSpawnerEnum(initialDelay, interval));
}
private IEnumerator StartSpawnerEnum(float initialDelay, float interval, bool initialCall = true)
{
if (initialCall) yield return new WaitForSeconds(initialDelay);
int positionRng = Random.Range(0, 1000);
GameObject obstacle = Instantiate(positionRng % 2 == 0 ? obstaclePrefabRight : obstaclePrefabLeft, transform);
obstacle.transform.localPosition = Vector3.zero;//SET POSITION ON ROAD
_spawnedObjectCount++;
if(obstacle.TryGetComponent(out TMP_Text tmpText))
{
tmpText.text = _spawnedObjectCount.ToString();
}
if (IsGameOver) yield break;
yield return new WaitForSeconds(interval);
StartCoroutine(StartSpawnerEnum(initialDelay, interval, false));
}
If there are things you do not understand or want me to elaborate on, you can ask.
Thanks a lot, Ogulcan. I just changed and add some things and now its working fine according to my requirements.
[SerializeField] private GameObject obstaclePrefabRight; [SerializeField] private GameObject obstaclePrefabLeft;
private TextMesh rightText;
private TextMesh leftText;
public int textValueRight;
public int textValueLeft;
public bool IsGameOver { get; set; } = false;
void Start()
{
textValueLeft = 2;
textValueRight = 1;
StartObstaclesSpawner(1, 1);
}
public void StartObstaclesSpawner(float initialDelay, float interval)
{
StartCoroutine(StartObsSpawnerEnum(initialDelay, interval));
}
IEnumerator StartObsSpawnerEnum(float initialDelay, float interval, bool initialCall = true)
{
if (initialCall)
{
yield return new WaitForSeconds(initialDelay);
}
Vector3 spawnPosRight = new Vector3(1.4f, 1.25f, 60f);
rightText = obstaclePrefabRight.GetComponentInChildren<TextMesh>();
rightText.text = textValueRight.ToString();
textValueRight += 2;
GameObject obstacleRight = Instantiate(obstaclePrefabRight, spawnPosRight, obstaclePrefabRight.transform.rotation);
Debug.Log(textValueRight);
Vector3 spawnPosLeft = new Vector3(-1.4f, 1.25f, 70f);
leftText = obstaclePrefabLeft.GetComponentInChildren<TextMesh>();
leftText.text = textValueLeft.ToString();
textValueLeft += 2;
GameObject obstacleLeft = Instantiate(obstaclePrefabLeft, spawnPosLeft, obstaclePrefabLeft.transform.rotation);
Debug.Log(textValueLeft);
if (IsGameOver)
{
yield break;
}
yield return new WaitForSeconds(interval);
StartCoroutine(StartObsSpawnerEnum(initialDelay, interval, false));
}
Please elaborate more step by step(if possible) as I am still facing the same problem of numbering. The obstacle spawning problem is solved but numbering on obstacles and increasing over each obstacle alternately on right-left is persist.
@faizanahmed089 Can you briefly explain how you set the text of the objects or show some sample code?
ps. If you want to increase obstacle numbering on each spawn, increase _spawnedObjectCount after each Instantiate calls.
Now it's done after some changes again.
Answer by logicandchaos · Apr 17 at 05:28 PM
Increasing number functionality is really easy, just make an integer and when you spawn an obstacle you tick it up. Best to make it a static int in the script for the obsticle, then you can tick it up in the OnEnable method.
Your answer

Follow this Question
Related Questions
Scene changing and multiple spawn points 1 Answer
How do i spawn in a prefab with a x,y,z location? 2 Answers
Trying to spawn enemies 1 Answer
How to Instantiate prefabs with function Update? 1 Answer
Copy Objects While In Test? 1 Answer