I have a script called WinCondition and a Script called LoseCondition They are not connected in any way but they are almost carbon copies of one another with a slight difference in the name of the functions and the script’s values they check.
WinCondition class
public class WinCondition : MonoBehaviour
{
ObstacleVariables obstacleVariables;
[SerializeField] GameObject Result;
[SerializeField] Text GameResultText;
int maxPoints = 1000;
public bool checkWin()
{
if (obstacleVariables.count >= maxPoints)
{
return true;
}
else
{
return false;
}
}
void Start()
{
obstacleVariables = GameObject.Find("Obstacle_Spawner").GetComponent<ObstacleVariables>();
}
// Update is called once per frame
void Update()
{
if (checkWin())
{
GameResultText.text = "You Win!";
Result.SetActive(true);
Debug.Log("You win");
}
}
}
LoseConditions script:
public class LoseCondition : MonoBehaviour
{
PlayerVariables playerVariables;
[SerializeField]GameObject Result;
[SerializeField]Text GameResultText;
void Start()
{
playerVariables = GameObject.FindWithTag("Player").GetComponent<PlayerVariables>();
}
public bool checkLose()
{
if (playerVariables.playerCurrentHP <= 0)
{
return true;
}
else
{
return false;
}
}
// Update is called once per frame
void Update()
{
if (checkLose())
{
GameResultText.text = "You Lose!";
Result.SetActive(true);
Debug.Log("You Lose");
}
}
}
I have set the instances in the scene editor to what I wanted for both scripts. However, The compiler keeps saying there is an instance error with these two lines in WinCondition:
GameResultText.text = "You Win!";
Result.SetActive(true);
But there is no instance error with the almost identical two lines in the lose script:
GameResultText.text = "You Lose!";
Result.SetActive(true);