I have an problem where I instantiate a GameObject and it keeps on instantiating a million times over, even though I put the line of code under the start function.
I am trying to have the player collide with the coin(then a point is added to your score) , then the coin deletes itself and then re-instantiates at a random coordinate, but I cant get past the initial instantiation.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Coin : MonoBehaviour {
public GameObject coins;
Vector2 randomPosition;
public UIScript _UIScript;
// Use this for initialization
void Start () {
_UIScript = GameObject.FindWithTag("UI").GetComponent<UIScript>();
randomPosition = new Vector2(Random.Range(-8f, 8), Random.Range(-4f, 4f));
Instantiate(coins, randomPosition, Quaternion.identity);
}
// Update is called once per frame
void Update () {
}
private void OnTriggerEnter2D(Collider2D coinCollision)
{
switch(coinCollision.tag)
{
case "Player":
collisionWithPlayer();
break;
}
}
private void collisionWithPlayer()
{
_UIScript.highScore = _UIScript.highScore + 1;
Object.Destroy(coins);
}
}
public GameObject coinscontains game objects of typepublic class Coin, and thatStartis called when instantiated, you have an infinite loop of creation of objects. Maybe you could put the call to instantiate incollisionWithPlayerinstead, just before callingObject.Destroy(coins);. \$\endgroup\$