- Home /
Question by
Hobene · Apr 21, 2020 at 11:27 AM ·
gameobjectprefabarray
How can I instantiate a single prefab from an array?
I have a code that spawns obstacles in front of the player. It has an array of obstacle prefabs, and chooses 1 random obstacle from that array. I have made a count int into to the game that look how many prefabs can be instantiated. I also put a destroy with tag if the count is crossed. But now it just instantiates bunch of obstacles and deletes them "1 second" later.
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;
public bool mängAlgas = false; //game began bool
public bool round2 = false;
public GameObject Player;
public int maximumObj;
public GameObject[] howManyTreesHaveBeenInstantiated;
public AudioSource mainTheme;
private int count = 0;
public AudioSource jumpSound;
public GameObject surmaLaulja;
public GameObject ScoreCounter;
// Start is called before the first frame update
void Start()
{
surmaLaulja.SetActive(false);
if (mängAlgas != true)
{
return;
}
else if (mängAlgas == true)
{
mainTheme.Play();
StartCoroutine("SpawnDelay");
//return;
}
}
void Update()
{
SpawnDelay();
howManyTreesHaveBeenInstantiated = GameObject.FindGameObjectsWithTag("Tree");
count = howManyTreesHaveBeenInstantiated.Length;
if (ScoreCounter.GetComponent<ScoreCounter>().mängLäbi == true)
{
surmaLaulja.SetActive(true);
}
#if UNITY_EDITOR
DetectMousePress();
#else
DetectTouchPress();
#endif
SwipeEnded();
}
private void SpawnTrees()
{
if (count <= 1)
{
print("I'm Spawning a tree.");
float randomXValue = Random.Range(927f, 1500f);
Vector2 spawnPos = new Vector2(randomXValue, 50f);
Instantiate(PickRandomTree(), spawnPos, Quaternion.identity);
}
else if ( >= count)
{
Destroy(GameObject.FindGameObjectWithTag("Tree"));
}
}
private GameObject PickRandomTree()
{
int randomNumber = Random.Range(0, trees.Length);
GameObject treeThatWeChose = trees[randomNumber];
return treeThatWeChose;
}
public void DetectTouchPress()
{
foreach (Touch touch in Input.touches)
{
if (touch.phase == TouchPhase.Began)
{
jumpSound.Play();
//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))
{
jumpSound.Play();
//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