0

В общем, я хочу, чтобы когда мой игрок касался кое какого объекта, то на экран выводил You Win. Так вот, You Lose, HP и Score отлично работают, но, когда приходит момент, когда экран должен написать You Win выводит эту ошибку "NullReferenceException: Object reference not set to an instance of an object test.OnCollisionEnter (UnityEngine.Collision col) (at Assets/test.cs:81) " Вот весь код.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class test : MonoBehaviour { public int HP; public int Score; public float Fall;

public Text HP_text; public Text Score_text; public Text YouLose_text; public Text Win_text; void Start() { HP = 100; Score = 0;

HP_text = GameObject.FindWithTag("HP").GetComponent<Text>();
Score_text = GameObject.FindWithTag("Score").GetComponent<Text>();
YouLose_text = GameObject.FindWithTag("YouLose").GetComponent<Text>();
Win_text = GameObject.FindWithTag("Win").GetComponent<Text>();

}

void Update() { HP_text.text = "HP:" + HP; Score_text.text = "Score:" + Score;

if (HP > 100)
{
    HP = 100;
}

if (HP < 0)
{
    HP = 0;
}

if (HP <= 0)
{
    YouLose_text.text = "You Lose";
}

Vector3 Checkpoint = new Vector3 (0,2,0);
GameObject Player = GameObject.FindGameObjectWithTag("Player");
Fall = GameObject.FindGameObjectWithTag("Player").transform.position.y;
if (Fall<-4) 
{
    HP -= 10;
    Player.transform.position = Checkpoint;
}

}

private void OnTriggerEnter(Collider other) { if (other.tag == "Coin") { Score += 1; Destroy(other.gameObject); } if (other.tag == "MedKit") { HP += 10; Destroy(other.gameObject); } }

private void OnCollisionEnter(Collision col) { if (col.gameObject.tag == "You Lose") { YouLose_text.text = "You Lose"; } if (col.gameObject.tag == "Win") { Win_text.text = "You Win"; } } }

Tigran
  • 1
  • 4

0 Answers0