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();
}
// 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().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);
}
}
↧