Hi, Can someone help me with this, I’m trying to understand why my script won’t work, I’m new to coding so if someone can help me figure out this problem I have that’d be nice.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
public class ctf : MonoBehaviour
{
public bool blueTeam = false;
public bool redTeam = false;
private float _redCapturePerc = 0;
private float _blueCapturePerc = 0;
public GameObject NuetralFlag;
public GameObject redFlag;
public GameObject Blueflag;
public float Timer;
public Text Notifier;
public Text timer;
public GameObject RedTeamWins, BlueTeamWins, TieScreen;
public Button quit;
void Start()
{
redFlag.GetComponent<MeshRenderer>().enabled = false;
Blueflag.GetComponent<MeshRenderer>().enabled = false;
NuetralFlag.GetComponent<MeshRenderer>().enabled = true;
quit.gameObject.SetActive(false);
}
void Update()
{
if (blueTeam == true)
{
_blueCapturePerc += Time.deltaTime * 5;
_redCapturePerc -= Time.deltaTime * 5;
}
if (redTeam == true)
{
_redCapturePerc += Time.deltaTime * 5;
_blueCapturePerc -= Time.deltaTime * 5;
}
if (redTeam == true && blueTeam == true)
{
_redCapturePerc = _redCapturePerc;
_blueCapturePerc = _blueCapturePerc;
}
if (_blueCapturePerc >= 100)
{
_blueCapturePerc = 100;
Blueflag.GetComponent<MeshRenderer>().enabled = true;
redFlag.GetComponent<MeshRenderer>().enabled = false;
NuetralFlag.GetComponent<MeshRenderer>().enabled = false;
}
if (_redCapturePerc >= 100)
{
_redCapturePerc = 100;
redFlag.GetComponent<MeshRenderer>().enabled = true;
Blueflag.GetComponent<MeshRenderer>().enabled = false;
NuetralFlag.GetComponent<MeshRenderer>().enabled = false;
}
if (_blueCapturePerc <= 50 && _redCapturePerc <= 51)
{
_blueCapturePerc = 100;
_redCapturePerc = 100;
Blueflag.GetComponent<MeshRenderer>().enabled = false;
redFlag.GetComponent<MeshRenderer>().enabled = false;
NuetralFlag.GetComponent<MeshRenderer>().enabled = true;
}
if (_blueCapturePerc <= 0)
{
_blueCapturePerc = 0;
}
if (_redCapturePerc <= 0)
{
_redCapturePerc = 0;
}
if (Blueflag.GetComponent<MeshRenderer>().enabled == true)
{
Notifier.text = "Blue Team has taken over Red Teams Flag";
}
if (redFlag.GetComponent<MeshRenderer>().enabled == true)
{
Notifier.text = "Red Team has Taken over Blue Teams Flag";
}
if (Timer > 0)
{
Timer -= Time.deltaTime;
}
if (Timer <= 0 && Blueflag.GetComponent<MeshRenderer>().enabled == true)
{
Timer = 0;
BlueTeamWins.SetActive(true);
quit.gameObject.SetActive(true);
Time.timeScale = 0;
Notifier.enabled = false;
timer.enabled = false;
}
if (Timer <= 0 && redFlag.GetComponent<MeshRenderer>().enabled == true)
{
Timer = 0;
RedTeamWins.SetActive(true);
quit.gameObject.SetActive(true);
Time.timeScale = 0;
Notifier.enabled = false;
timer.enabled = false;
}
if (Timer <= 0 && redFlag.GetComponent<MeshRenderer>().enabled == false && Blueflag.GetComponent<MeshRenderer>().enabled == false && NuetralFlag.GetComponent<MeshRenderer>().enabled == true)
{
Timer = 0;
TieScreen.SetActive(true);
quit.gameObject.SetActive(true);
Time.timeScale = 0;
Notifier.enabled = false;
timer.enabled = false;
}
timer.text = Timer.ToString("Time: " + Timer);
quit.onClick.AddListener(Quit);
}
void Quit()
{
SceneManager.LoadScene("Main Menu");
// UnityEditor.EditorApplication.isPlaying = false;
}
}
heres my other script also,
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class capController : MonoBehaviour
{
private ctf capControllerr;
void Start()
{
capControllerr = GameObject.Find("Player").GetComponent<ctf>();
}
private void OnTriggerEnter(Collider other)
{
if (other.tag == "BlueTeam")
{
capControllerr.blueTeam = true;
}
if (other.tag == "RedTeam")
{
capControllerr.redTeam = true;
}
}
private void OnTriggerExit(Collider other)
{
if (other.tag == "BlueTeam")
{
capControllerr.blueTeam = true;
}
if (other.tag == "RedTeam")
{
capControllerr.redTeam = true;
}
}
}