Ask coding questions

← Back to all posts
Unity Death Wall
darkdarcool (130)

Hiya!

So, I asked a question about a death wall about a week ago, and it worked great. My game was coming along. But I discovered... that it was really hard. So, I decided to make a death counter. It worked great I accessed a UI textbox, and it counted deaths. But only for that certain wall. Say I would jump into a death wall. It would go up on deaths. And It would keep going up when I hit that wall. But when I touch a different death wall. It will show me that walls death count. And, this is not what I wanted. I don't want a death count for just a certain wall. I want it for every wall. Anyway. This is my script:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class Respawn : MonoBehaviour
{
		int deaths;
		public Text textbox;	
		
	
    public Transform player;
		//public DeathShower other;
    public Transform respawnPoint;

    private void OnTriggerEnter(Collider other) // On touch of the wall
    {
			/*
			Just to figure out who hit the death wall!
			Debug.Log(other.name);
			*/
			if (other.name == "Player") {
				// Event where the player hits a death wall
				player.transform.position = respawnPoint.transform.position;
				deaths++;
			  textbox.text = "Deaths: " + deaths;

				
				
			}
			else {
				// This is the even where the bullet or anything with a collider hits a death wall
			}
			
        
    }		
}

Can anyone help me make this not a certain wall death counter?

Thanks!

Comments
hotnewtop
Coder100 (18101)

the easiest way is to reference it in a different class, most people would set this in the GameManager class to Player. The reason why it does for a specific wall is that each wall gets its own instance.

btw why have an else lol

SixBeeps (5223)

You'll need a script on the player which'll keep track of the deaths. Then, you can call a function on that script from the current one you have on the wall. Something like

in Player.cs

public int deaths = 0;
public Text deathText;

public void KillMe() {
  deaths++;
  deathText.text = "Deaths: " + deaths;
}

in Wall.cs

private void OnTriggerEnter(Collider other) {
  if (other.name == "Player") {
    other.GetComponent<Player>().KillMe();
  }
}
darkdarcool (130)

@SixBeeps
It gives me this error:
The type or namespace name 'Player' could not be found (are you missing a using directive or an assembly reference?)

SixBeeps (5223)

@darkdarcool My bad, I should have mentioned that Player.cs and Wall.cs are not only arbitrary file names, but are also incomplete. They'd go into their respective pre-existing scripts.

darkdarcool (130)

@SixBeeps
I knew that were, different, so I changed the names or the scripts because it would mean less work and stuff. It still gives me an error