Reputation: 491
I have been trying to make a score/points system for my game where every time an object has been destroyed the score increases, I'm using a set of assets named GDG_Assets which allow you to create destructible environmental objects. when an object is hit the main object is made invisible and a model of the object broken up is spawned in. I want to know how I can detect when these new models have been spawned in and how I can implement them into my code? The "Amount" ints are where the number of objects destroyed/spawned will be stored to give a score. I could detect collision with my projectile but the issue with this is that the way the game is designed an explosion causes a chain reaction causing other objects to be destroyed. any ideas or code improvements would be much appreciated.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class scoreCount : MonoBehaviour {
public Text countText;
public int eggCount;
public int barrelCount;
public int cubeCount;
//game objects
public GameObject Egg_000;
public GameObject Barrel_000;
public GameObject Cube01_000;
//amount of objects destroyed
public int barrelAmount;
public int eggAmount;
public int cubeAmount;
private int count;
private int egg;
private int barrel;
private int cube;
// Initializing points of each object
void Start () {
count = 0;
egg = 100;
barrel = 75;
cube = 200;
countText.text = "Score: " + count.ToString ();
}
// Update is called once per frame
void Update () {
//multiplies amount of objects destroyed by points
eggCount = egg * eggAmount;
barrelCount = barrel * barrelAmount;
cubeCount = cube * cubeAmount;
//determines final score
count = eggCount + barrelCount + cubeCount;
countText.text = "Score: " + count.ToString();
}
}
Upvotes: 0
Views: 1521
Reputation: 90659
You should make that more event driven instead of having it in Update
therefore I would implement an enum
.
public enum DestructableType
{
Egg,
Barrel,
Cube,
}
Than I would change your class like
public class scoreCount : MonoBehaviour
{
[Header("Components")]
public Text countText;
[Header("Prefabs")]
public DestructableObject Egg_000;
public DestructableObject Barrel_000;
public DestructableObject Cube01_000;
// I didn't see what those are used for
/*
public int eggCount;
public int barrelCount;
public int cubeCount;
*/
[Header("amount of objects destroyed")]
public int barrelAmount;
public int eggAmount;
public int cubeAmount;
// Simply adjust those in the inspector as well and give them default values
[Header("points values")]
[SerializeField] private int egg = 100;
[SerializeField] private int barrel = 75;
[SerializeField] private int cube = 200;
// Will be 0 by default anyway
private int count;
// Initializing points of each object
private void Start ()
{
// Only thing needed to be set in Start
// though even this you could simply do in that Text component already
countText.text = "Score: " + count.ToString ();
}
// Called by the DestructableObjects when destroyed
public void ObjectsDestroyed(DestructableType type)
{
// I didn't know which values you really need in the end
// actually the lines with count += xyz; would be enough I guess
switch(type)
{
case Egg:
eggCount += egg;
eggAmount++;
count += egg;
break;
case Barrel:
barrelCount += barrel;
barrelAmount++;
count += barrel;
break;
case Cube:
cubeCount += cube;
cubeAmount++;
count += cube;
break;
}
countText.text = "Score: " + count.ToString();
}
}
And on your Prefabs add an additional component
public class DestructableObject : MonoBehaviour
{
// Adjust this through the inspector
public DestructableType Type;
private void OnDestroy ()
{
FindObjectOfType<scoreCount>().ObjectsDestroyed(Type);
}
}
Upvotes: 1