• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
Question by TwoCoolCole12 · Dec 15, 2021 at 11:27 AM · errorgamenot working

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 $$anonymous$$t 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 not$$anonymous$$ng happens. The weird t$$anonymous$$ng is, there are no errors. I also want my game to stop when. my health reaches zero, but not$$anonymous$$ng 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);
     }
 }

}

Comment

People who like this

0 Show 0
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

199 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

URP Baked lighting makes the colour blue turn black 0 Answers

Raycast hits and affects all items within the same tag 2 Answers

Collision Causing Lag? 1 Answer

Why doesnt my script work 2 Answers

Plug in for tutorial videos 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges