- Home /
gameobjects don't add to count when destroyed
In my game, the enemies have to get to the 'trigger' and if they get there the count will go up by 1 but when they get destroyed the count doesn't go up at all. I don't know what's happening and I'm not familiar with coding so please help
script for enemies
using System.Collections; using System.Collections.Generic; using UnityEngine;
public class CubeControl : MonoBehaviour {
public Transform target; public float speed; private int current;
public int count; public Text countText; public Text losetext; public Rigidbody rb;
// Use this for initialization
void Start()
{
rb = GetComponent<Rigidbody>();
}
// Update is called once per frame
void Update()
{
// The step size is equal to speed times frame time.
float step = speed * Time.deltaTime;
// Move our position a step closer to the target.
transform.position = Vector3.MoveTowards(transform.position, target.position, step);
}
}
script for trigger
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class trigger : MonoBehaviour {
public int count;
public Text countText;
public Text losetext;
// Use this for initialization
void Start ()
{
count = 0;
SetCountText();
losetext.text = "";
}
void OnTriggerEnter(Collider other)
{
if (other.GetComponent<Collider>().tag == "enemy")
{
Destroy(other.gameObject);
count = count + 1;
SetCountText();
}
}
void SetCountText()
{
countText.text = "Count: " + count.ToString();
if (count >= 10)
{
losetext.text = "Game Over!";
}
}
}
script for spawner
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
public class spawner : MonoBehaviour
{
public GameObject objectToSpawn;
public GameObject newobject;
// Use this for initialization
void Start()
{
InvokeRepeating("SpawnObject", 1f, 1f);
}
// Update is called once per frame
void SpawnObject()
{
float randomX = Random.Range(387, 120);
Vector3 randomLocation = new Vector3(randomX, 3, 455);
newobject = Instantiate(objectToSpawn, randomLocation, transform.rotation);
}
}
I don't see any reason why this wouldn't update the count in the Trigger Class - which I am assu$$anonymous$$g is your intentions.
Also, you should change your if statement to this:
if (other.CompareTag("enemy"))
No reason to grab the collider for the tag. Your object already has it.
Answer by dan_wipf · Dec 07, 2018 at 07:16 AM
well you destroy the gameobject befor the counter. so the if statement(collider.tag == enemy) will turn false as soon as the gameobject is destoyed, and further code is not reachable. set the destroy gameobject after SetCountText(); and it should work
This is not true. Once the if statement is evaluated, you are in a code block... it doesn't come back out and get reevaluated because the if statement object was destroyed.
Actually he doesn't destroy this gameobject but the "other". However a possible issue is that he has seperate "count" and those "Text" references in his "CubeControl" class and in his "trigger" class. Those do not belong to each other. Those are two seperate count variables. However he doesn't seem to use the one inside CubeControl if this is the whole script.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers