- Home /
Many issues
Hi, I'm a new developer and I am creating a custom game for my class. I have basic coding down and it works great. There are just a couple issues. When I run into a health power up, my health doesn't go up. It does, however, go down when I hit an enemy. I also want my damage to the enemy to change depending on what power up bool is active in my player controller. However nothing happens. The weird thing is, there are no errors. I also want my game to stop when. my health reaches zero, but nothing happens. It only says that there is a null reference in my game manager on line 25. If anyone can help, it will be much appreciated. Here are my scripts.
Player Health:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class PlayerHealth : MonoBehaviour { public float health; public Slider slider; public float lifeUp = 30f; public float contactDamage = 5f;
// Start is called before the first frame update
void Start()
{
}
// Update is called once per frame
void Update()
{
slider.value = health;
}
void OnCollisionEnter(Collision obj)
{
if(obj.gameObject.tag == "Enemy")
{
health = health - contactDamage;
}
if(obj.gameObject.tag == "Life")
{
health = health + lifeUp;
Debug.Log("Health Up");
}
}
}
Enemy Health:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class EnemyHealth : MonoBehaviour { public float health; public Slider slider; private PlayerController playerController; public float currentDamage; public float speedDamage = 40f; public float strengthDamage = 50f; public float normalDamage = 20f;
// Start is called before the first frame update
void Start()
{
playerController = GetComponent<PlayerController>();
}
// Update is called once per frame
void Update()
{
slider.value = health;
if (health <= 0)
{
Destroy(gameObject);
}
}
void OnCollisionEnter(Collision obj)
{
if (obj.gameObject.tag == "Player")
{
if (obj.gameObject.GetComponent<PlayerController>().hasPowerupStrength == true)
{
currentDamage = strengthDamage;
Debug.Log("Damage Change");
}
else
{
currentDamage = normalDamage;
}
if (obj.gameObject.GetComponent<PlayerController>().hasPowerupSpeed == true)
{
currentDamage = speedDamage;
Debug.Log("Damage Change");
}
else
{
currentDamage = normalDamage;
}
health = health - currentDamage;
}
}
}
Player Controller:
using System.Collections; using System.Collections.Generic; using UnityEngine; using TMPro;
public class PlayerController : MonoBehaviour { public float speedMove = 20; public float speedOrigin = 20; public float newspeed = 40; public float horizontalInput; public float verticalInput; public float xRange = 30; public float zBound = 30; public AudioClip wallHit; private AudioSource playerAudio; public bool hasPowerupSpeed; public bool hasPowerupStrength; public bool HasPowerupHealth; public GameObject PowerupSpeed; public GameObject PowerupStrength; public GameObject PowerupLife; private GameManager gameManager; public ParticleSystem fastParticle; public ParticleSystem normalParticle;
// Start is called before the first frame update
void Start()
{
fastParticle.Stop();
gameManager = GameObject.Find("Game Manager").GetComponent<GameManager>();
playerAudio = GetComponent<AudioSource>();
PowerupStrength = GameObject.Find("PowerupStrength");
PowerupSpeed = GameObject.Find("PowerupSpeed");
PowerupLife = GameObject.Find("PowerupLife");
normalParticle.Play();
}
// Update is called once per frame
void Update()
{
//Get key input
horizontalInput = Input.GetAxis("Horizontal");
verticalInput = Input.GetAxis("Vertical");
//Move the player around
transform.Translate(Vector3.forward * Time.deltaTime * speedMove * verticalInput);
transform.Translate(Vector3.right * Time.deltaTime * speedMove * horizontalInput);
//Boundaries
if (transform.position.x < -xRange)
{
transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
playerAudio.PlayOneShot(wallHit, 1.0f);
}
if (transform.position.x > xRange)
{
transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
playerAudio.PlayOneShot(WallHit, 1.0f);
}
if (transform.position.z < -zBound)
{
transform.position = new Vector3(transform.position.x, transform.position.y, -zBound);
playerAudio.PlayOneShot(WallHit, 1.0f);
}
if (transform.position.z > zBound)
{
transform.position = new Vector3(transform.position.x, transform.position.y, zBound);
playerAudio.PlayOneShot(WallHit, 1.0f);
}
}
private void OnTriggerEnter(Collider other)
{
//Destroy powerup if collected and start timer
if (other.CompareTag("Speed"))
{
hasPowerupSpeed = true;
Destroy(other.gameObject);
StartCoroutine(SpeedPowerupCountdownRoutine());
}
//Destroy powerup if collected and start timer
if (other.CompareTag("Strength"))
{
hasPowerupSpeed = true;
Destroy(other.gameObject);
StartCoroutine(StrengthPowerupCountdownRoutine());
}
//Destroy powerup if collected and start timer
if (other.CompareTag("Life"))
{
HasPowerupHealth = true;
Destroy(other.gameObject);
HasPowerupHealth = false;
}
}
IEnumerator SpeedPowerupCountdownRoutine()
{
//Countdown start and stop
speedMove = newspeed;
normalParticle.Stop();
fastParticle.Play();
yield return new WaitForSeconds(7);
speedMove = speedOrigin;
fastParticle.Stop();
hasPowerupSpeed = false;
normalParticle.Play();
}
IEnumerator StrengthPowerupCountdownRoutine()
{
//Countdown start and stop
yield return new WaitForSeconds(7);
hasPowerupStrength = false;
}
}
Game Manger:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; using UnityEngine.UI; using TMPro;
public class GameManager : MonoBehaviour { public Button restartButton; public TextMeshProUGUI gameOverText; public bool isGameActive; private PlayerHealth playerhealth;
// Start is called before the first frame update
void Start()
{
isGameActive = true;
playerhealth = GetComponent<PlayerHealth>();
}
// Update is called once per frame
void Update()
{
if (playerhealth.health <= 0)
{
GameOver();
}
}
public void GameOver()
{
gameOverText.gameObject.SetActive(true);
isGameActive = false;
restartButton.gameObject.SetActive(true);
}
public void RestartGame()
{
SceneManager.LoadScene(SceneManager.GetActiveScene().name);
}
}
Enemy:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Enemy : MonoBehaviour { public float speed = 20.0f; private Rigidbody enemyRb; private GameObject player; public float xRange = 30; public float zBound = 30; public AudioClip WallHit; private AudioSource playerAudio; public ParticleSystem enemyParticle;
// Start is called before the first frame update
void Start()
{
//Variable meaning
enemyRb = GetComponent<Rigidbody>();
player = GameObject.Find("Player");
playerAudio = GetComponent<AudioSource>();
enemyParticle.Play();
}
// Update is called once per frame
void Update()
{
//Find the player
Vector3 lookDirection = (player.transform.position - transform.position).normalized;
//Follow player
enemyRb.AddForce(lookDirection * speed);
//Boundaries1
if (transform.position.x < -xRange)
{
transform.position = new Vector3(-xRange, transform.position.y, transform.position.z);
playerAudio.PlayOneShot(WallHit, 1.0f);
}
if (transform.position.x > xRange)
{
transform.position = new Vector3(xRange, transform.position.y, transform.position.z);
playerAudio.PlayOneShot(WallHit, 1.0f);
}
if (transform.position.z < -zBound)
{
transform.position = new Vector3(transform.position.x, transform.position.y, -zBound);
playerAudio.PlayOneShot(WallHit, 1.0f);
}
if (transform.position.z > zBound)
{
transform.position = new Vector3(transform.position.x, transform.position.y, zBound);
playerAudio.PlayOneShot(WallHit, 1.0f);
}
}
}
Your answer
![](https://koobas.hobune.stream/wayback/20220613050544im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
[Closed] "The regerenced script on the Behavior is missing!" 1 Answer
Error UCE0001 ";"Expected 2 Answers
Why do I get these codes when starting the game? 0 Answers
unity not working help 2 Answers
Unity 5.1.3f1 won't open 0 Answers