instantiate keeps repeating itself
I'm new to unity and c# but I have been trying to recreate the game color switch. I followed the tutorial online about how to make the wheels, their rotations, collisions and the basic player mechanics. However, the tutorial did not show how to make the game infinite therefore for the past 9 hours I have been trying to accomplish that. I keep running into the same problem where the rings (only the larger one inside the smaller one) spawn multiple times when the green dot which gives the point and spawns in a new circle is consumed. I know there is probably a stupid error and you think I'm just trying to waist peoples time however I am really stuck and any help is appreciated. Thank you.`
using UnityEngine;
using UnityEngine.SceneManagement;
public class Player : MonoBehaviour {
public float jumpForce = 7f;
public Rigidbody2D rb;
public SpriteRenderer sr;
public string currentColor;
public GameObject Green;
public GameObject[] obj;
public float SpawnAmount = 1f;
public Color ColorBlue;
public Color ColorPurple;
public Color ColorYellow;
public Color ColorPink;
public int Score = 0;
public int Height = 0;
Vector3 CurrentSpawnPoint;
private void Start()
{
SetRandomColor();
}
void Update() {
if (Input.GetButtonDown("Jump") || Input.GetMouseButtonDown(0))
{
rb.velocity = Vector2.up * jumpForce;
}
}
void OnTriggerEnter2D(Collider2D col)
{
if (col.tag == "Change")
{
SetRandomColor();
Destroy(col.gameObject);
return;
}
else if (col.tag == "Point")
{
Debug.Log("Point gained");
Destroy(col.gameObject);
Score += 1;
Height += 7;
CurrentSpawnPoint = col.transform.position;
CurrentSpawnPoint.y += 7;
Instantiate(Green, CurrentSpawnPoint, Quaternion.identity);
Instantiate(obj[Random.Range(0, obj.GetLength(0))], CurrentSpawnPoint, Quaternion.identity);
Debug.Log(Height);
Debug.Log(CurrentSpawnPoint);
return;
}
else if (col.tag != currentColor)
{
Debug.Log("GAME OVER");
SceneManager.LoadScene(SceneManager.GetActiveScene().buildIndex);
}
}
void SetRandomColor()
{
int index = Random.Range(0, 4);
switch (index)
{
case 0:
currentColor = "Blue";
sr.color = ColorBlue;
break;
case 1:
currentColor = "Yellow";
sr.color = ColorYellow;
break;
case 2:
currentColor = "Purple";
sr.color = ColorPurple;
break;
case 3:
currentColor = "Pink";
sr.color = ColorPink;
break;
}
}
}`
Well, do you have any script attached to the "Green" object or the other one that might do somethings in the Start function?
Also, is there any more object that have the tag "Points" that should not have it, ie the floor?
Pretty sure what happen is when you instaniate another GameObject. Enter2D called again for the new gameobject.
This part:
Instantiate(Green, CurrentSpawnPoint, Quaternion.identity);
Instantiate(obj[Random.Range(0, obj.GetLength(0))], CurrentSpawnPoint, Quaternion.identity);
So what happen is when NewGameObject instaniated. Your Wheel call trigger too with the new gameobject. => Call Trigger again => make a new gameobject again -> infinite loop
Probably, but the destroy is called before the gameobjects are instantiated.
Destroy called after update() done. He should have call Destroy immediately