- Home /
How to Instantiate single Prefabs from an array?
I'm trying to make a android game that is recording score with time, and the player needs to jump over obstacles as the speed get's faster in game.
But my spawn seems to instantiate all of the array and even more prefabs and then stop indefinitely.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ObstacleSpawner : MonoBehaviour
{
//For the start of the game a swipe must be done
private Vector2 swipeStartPos;
private Vector2 swipeEndPos;
//array for obstacleprefabs trees
public GameObject[] trees;
int puud = 0;
int maxPuud = 3;
//second array for enemy prefabs
public GameObject[] Enemies;
int vastased = 0;
int maxVastased = 1;
public bool mängAlgas = false;
public bool round2 = false;
public GameObject Player;
public int maximumObj;
// Start is called before the first frame update
void Start()
{
if (mängAlgas == true)
{
StartCoroutine(WaitForSecondsAndThenSpawn());
}
}
void Update()
{
#if UNITY_EDITOR
SwipeEnded();
DetectMousePress();
#else
DetectTouchPress();
#endif
}
private void SpawnTrees()
{
if (puud <= maxPuud)
{
print("I'm Spawning a tree.");
float randomXValue = Random.Range(927f, 1500f);
Vector2 spawnPos = new Vector2(randomXValue, 50f);
Instantiate(PickRandomTree(), spawnPos, Quaternion.identity);
puud++;
}
}
//private void SpawnEnemies()
//{
// if (round2 == true)
// {
// if (vastased <= maxVastased)
// {
// print("yo ma spawnin vastase");
// //sündimis vahe x teljel
// float randomXValue = Random.Range(927f, 1500f);
// //Sündimis asukoht
// Vector2 spawnPos = new Vector2(randomXValue, 6.5f);
// //sünnitamine
// Instantiate(PickRandomEnemy(), spawnPos, Quaternion.identity);
// vastased++;
// }
// }
//}
private GameObject PickRandomTree()
{
int randomNumber = Random.Range(0, trees.Length);
GameObject treeThatWeChose = trees[randomNumber];
return treeThatWeChose;
}
//SUVALISE VASTASE VALIMISEKS
//private GameObject PickRandomEnemy()
//{
// int randomNumber = Random.Range(0, Enemises.Length);
// GameObject EnemiesThatWeChose = Enemies[randomNumber];
// return EnemiesThatWeChose;
//}
IEnumerator WaitForSecondsAndThenSpawn()
{
yield return new WaitForSeconds(1.5f);
SpawnTrees();
StartCoroutine(WaitForSecondsAndThenSpawn());
}
public void DetectTouchPress()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
//start of the touch
print("Touch began at" + touch.position);
swipeStartPos = touch.position;
}
if (touch.phase == TouchPhase.Ended)
{
print("touch ended at" + touch.position);
swipeEndPos = touch.position;
}
}
}
//Mouse down - Mouse Up record
public void DetectMousePress()
{
//print("Detection Button Input");
if (Input.GetMouseButton(0))
{
//Start of the press
//print("Mouse button was pressed at " + Input.mousePosition);
swipeStartPos = Input.mousePosition;
}
if (Input.GetMouseButtonUp(0))
{
//End of the press
//print("Mouse button was released" + Input.mousePosition);
swipeEndPos = Input.mousePosition;
}
}
public void SwipeEnded()
{
Vector2 swipeVector = swipeEndPos - swipeStartPos;
if (Mathf.Abs(swipeVector.x) > Mathf.Abs(swipeVector.y))
{
print("swipe was horizontal");
if (swipeVector.x < 0f)
{
print("mäng algas");
mängAlgas = true;
Start();
}
}
}
}
Comment
Your answer
Follow this Question
Related Questions
NullReference when accessing GameObject in array (C#) 1 Answer
Will a large array of game objects slow my game down? 2 Answers
How would I find the prefab of a gameObject? 2 Answers
Unable to generate a proper engine etc... 0 Answers
Does deactivating a GameObject automatically stop its coroutines? 4 Answers