In this game i am making (for fun)
I have made this score system in where if you shoot the enemy, It will add 1 score. And the first time you shoot it, it works, but on the clones, instead of adding one it is setting it to one.
here is my code:
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEditorInternal;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class EnemyLife : MonoBehaviour
{
public GameObject enemy;
public int Score;
public Text ScoreText;
[Header("Spawn Locations")]
public GameObject s1;
public GameObject s2;
public GameObject s3;
public GameObject s4;
private int rng = 0;
private float cx;
private float cy;
private float cz;
private bool firstTimeAround;
// Start is called before the first frame update
void Start()
{
enemy = GameObject.Find("MainEnemy/Enemy (1)");
}
// Update is called once per frame
void Update()
{
rng += 1;
if (rng > 4)
{
rng = 1;
}
}
void OnCollisionEnter(Collision collision)
{
if (collision.gameObject.layer == 6)
{
if (rng == 1)
{
cx = s1.transform.position.x;
cy = s1.transform.position.y;
cz = s1.transform.position.z;
}
if (rng == 2)
{
cx = s1.transform.position.x;
cy = s1.transform.position.y;
cz = s1.transform.position.z;
}
if (rng == 3)
{
cx = s1.transform.position.x;
cy = s1.transform.position.y;
cz = s1.transform.position.z;
}
if (rng == 4)
{
cx = s4.transform.position.x;
cy = s4.transform.position.y;
cz = s4.transform.position.z;
}
if (firstTimeAround && Score == 1)
{
do {
Score += 1;
}
while (Score == 1);
}
else
{
Score += 1;
}
Destroy(gameObject);
ScoreText.text = "Score: " + Score.ToString();
Instantiate(enemy, new Vector3(cx, cy, cz), transform.rotation);
Debug.Log(Score);
firstTimeAround = true;
}
}
}
I have tried my own solutions (as you can see with the firsttimearound boolean).