I'm new to Unity and together with a few friends, I try to make a game. I was trying to connect the healthbar I made to the damage u can deal by shooting knifes. First to test this I used:
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
TakeDamage(25);
}
}
Everything worked fine and when I pressed Mouse1 the healthbar worked.My goal was, that when a knife hits the enemy 25 damage is dealt and the healthbar shows that the enemy lost health. However, when the knifes hit the enemy nothing happens. Can you please help me because I tried fixing this for like 3 hours and I'm getting kinda frustrated. Thank you.
Here's the code for the healthbar:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class healthbar : MonoBehaviour
{
public Slider slider;
public void SetMaxHealth(int health)
{
slider.maxValue = health;
slider.value = health;
}
public void SetHealth(int health)
{
slider.value = health;
}
}
And here's the code for the enemy:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Enemy_Follow : MonoBehaviour
{
public float speed;
public Transform target;
public int damage = 25;
public int maxHealth = 100;
public int currentHealth;
public healthbar healthBar;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
target = GameObject.FindGameObjectWithTag("Player").GetComponent<Transform>();
}
// Update is called once per frame
void Update()
{
if (Input.GetKeyDown(KeyCode.Mouse1))
{
TakeDamage(25);
}
transform.position = Vector2.MoveTowards(transform.position, target.position, speed * Time.deltaTime);
}
public void OnTriggerEnter2D(Collider2D other)
{
if (other.tag == "Knife")
{
TakeDamage(25);
}
}
public void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
}
After finding out that your colliders are not triggers it should rather be OnCollisionEnter2D like e.g.
public void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("Knife"))
{
TakeDamage(25);
}
}
See also Colliders -> Collision Action Matrix
You may try this code to look if the slider works at all. If this code does work (You can change things with the inspector) then it should be an error with the collision/trigger detection and nothing else.
public class SliderTest : MonoBehaviour {
public Slider slider;
public int maxHealth = 100;
private int currentHealth = 100;
public bool resetHealth = false;
public bool damage = false;
void Start() {
slider = GetComponent<Slider>();
SetMaxHealth(maxHealth);
}
void Update() {
if(resetHealth) {
resetHealth = false;
SetHealth(maxHealth);
}
if(damage) {
damage = false;
SetHealth(currentHealth-10);
}
}
public void SetMaxHealth(int health) {
slider.maxValue = health;
slider.value = 100;
}
public void SetHealth(int health) {
currentHealth = health;
slider.value = currentHealth;
}
}
}
Related
I am making health and damage script in Unity.
I have error CS0117 'PlayerHealth' does not contain a definition for 'TakeDamage'
I want player to have 20 healths and when touched an object it will damage him.
I tried searching on google but I didn't find answer.
PlayerHealth script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour
{
public int health = 20;
public void TakeDamage(int damage)
{
health -= damage;
if (health <= 0)
{
Destroy(gameObject, 0.3f);
}
}
}
And for EnemyDamage script I use this code:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PlayerHealth : MonoBehaviour
{
public int DMG;
float cooldown = 0f;
private void Update()
{
Debug.Log(cooldown);
cooldown -= Time.deltaTime;
}
private void OnCollisionEnter(Collision collision)
{
PlayerHealth health = collision.gameObject.GetComponent<PlayerHealth>();
if (cooldown <= 0)
{
if (health != null)
{
PlayerHealth.TakeDamage(DMG);//here is the error
}
cooldown = 1f;
}
}
}
PlayerHealth.TakeDamage(DMG); tries to call a static method TakeDamage on the class PlayerHealth but there is no such method, hence the error.
Your code is a little mess anyway, lets clean it up.
Attach this script to your game objects which can lose health (e.g. a player):
public class Health : MonoBehaviour // you called it NewBehaviourScript
{
public int health = 20;
public void TakeDamage(int damage) {
health -= damage;
if (health <= 0) {
Destroy(gameObject, 0.3f);
}
}
}
Attach this script to your game objects which can inflict damage:
public class Damage : MonoBehaviour // you called it PlayerHealth (?!)
{
public int DMG;
float cooldown = 0f;
void Update()
{
Debug.Log(cooldown);
cooldown -= Time.deltaTime;
}
void OnCollisionEnter(Collision collision)
{
if (cooldown <= 0f) {
Health health = collision.gameObject.GetComponent<Health>();
if (health != null) {
// call TakeDamage on the collided >instance<
health.TakeDamage(DMG);
}
cooldown = 1f;
}
}
}
This should give you a starting point with an appropriate colliders setup.
I am busy creating a health system and created a game over screen to show when current health is 0/player is dead.
But the game over screen is not showing (I don't know much about if)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.InputSystem;
public class HealthSystem : MonoBehaviour
{
public int maxHealth = 100;
public int currentHealth;
public HealthBar healthBar;
public GameObject gameOver;
public bool damage;
// Start is called before the first frame update
void Start()
{
currentHealth = maxHealth;
healthBar.SetMaxHealth(maxHealth);
}
void TakeDamage(int damage)
{
currentHealth -= damage;
healthBar.SetHealth(currentHealth);
}
public void GameOver()
{
if (currentHealth == 0)
{
gameOver.SetActive(true);
}
}
public void RestartButton()
{
SceneManager.LoadScene("Playground");
}
public void OnDamage(InputValue value)
{
DamageInput(value.isPressed);
TakeDamage(10);
}
public void DamageInput(bool newDamageState)
{
damage = newDamageState;
}
}
You are nowhere calling GameOver() in your code. Every time you decrement currentHealth(), check if it reached zero and do the necessary. For this, call GameOver() in your TakeDamage() method
void TakeDamage(int damage)
{
currentHealth -= damage;
GameOver();
healthBar.SetHealth(currentHealth);
}
Also, check if currentHealth is less than or equal to 0, not just 0
public void GameOver()
{
if (currentHealth <= 0)
{
gameOver.SetActive(true);
}
}
I am trying to decrease the healthbar of the player whenever he collides with an enemy laser but the script that I wrote is not working and it is giving me the following error:
Assets/Scripts/Laser/HealthManager.cs(19,21): error CS0029: Cannot implicitly convert type void' to UnityEngine.UI.Slider.
Can someone take some time and review my code and tell me why the healthbar is not working? Thank you.
HealthManager script :
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class HealthManager : MonoBehaviour {
public int CurrentHealth { get; set; }
public int MaxHealth { get; set; }
public Slider HealthBar;
//public GameObject LaserBulletEnemyPreFab;
//public GameObject PlayerPrefab;
//public LaserLevelManager myLevelmanager;
// Use this for initialization
void Start () {
MaxHealth = 20;
CurrentHealth = MaxHealth; //Resseting the health on start
HealthBar = CalculatingHealth();
}
// Update is called once per frame
void Update()
{
if(GetComponent<Collider>().gameObject.tag == "EnemyLaser")
{
Destroy(GetComponent<Collider>().gameObject);
DealDamage(1);
}
}
void DealDamage(int DamageValue)
{
CurrentHealth -= DamageValue; //Deduct the damage dealt from the player's health
HealthBar = CalculatingHealth();
if(CurrentHealth <= 0) //If health is 0
{
PlayerDead(); //Calling the function
}
}
void CalculatingHealth()
{
int healthdecrease = CurrentHealth / MaxHealth;
}
void PlayerDead()
{
CurrentHealth = 0; //Currenthealth is 0
LaserLevelManager.LoadLevel("Lose"); //Take player to the lose scene
}
}
HealthBar is of type Slider. CalculatingHealth is a function that does not return anything (so void).
You try to set a variable initialy initialized to type Slider to a void value. This is not possible.
You could:
float CalculatingHealth()
{
return healthdecrease = CurrentHealth / MaxHealth;
}
And
HealthBar.value = CalculatingHealth();
Note: https://docs.unity3d.com/ScriptReference/UI.Slider-value.html
I'm doing an educational game for kids..
But I stopped at the end of the scene
I could not make a code to start the new scene..
in the First Script
when play game the scenes does not stop until the last scene.
YouWin.pictureInPlace++;
I searched a lot and did not find my question, so I consulted you. It was easier to do a button to go to the Next scene but I prefer to do it automaticly
I think this task can be accomplished by the boolean but its need to reference game object.. and the script on 2 images.
The First Script (Manager) has put on Four images At Canvas..
The second one (YouWin) I put on empty GameObject..
thanks for the help
The first script (Manger)
using UnityEngine;
using UnityEngine.EventSystems;
public class Manager : MonoBehaviour, IBeginDragHandler, IDragHandler, IEndDragHandler
{
Vector2 pos1;
public GameObject pos2;
private bool canMove;
public GameObject winner;
void Start()
{
pos1 = transform.position;
canMove = true;
}
public void OnBeginDrag(PointerEventData eventData)
{
Debug.Log(eventData);
}
public void OnDrag(PointerEventData eventData)
{
if (canMove)
transform.position = Input.mousePosition;
}
public void OnEndDrag(PointerEventData eventData)
{
float distance = Vector3.Distance(transform.position, pos2.transform.position);
if (distance < 50)
{
transform.position = pos2.transform.position;
transform.localScale = pos2.transform.localScale;
canMove = false;
winner.GetComponent<YouWin>().pictureInPlace++;
}
else
{
transform.position = pos1;
}
}
}
The second script (YouWin)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class YouWin : MonoBehaviour
{
public int NumberOfImages;
public int pictureInPlace;
public string sceneName;
void Update()
{
if (pictureInPlace == NumberOfImages)
{
StartCoroutine(LoadScene());
Debug.Log("You Win!");
}
}
IEnumerator LoadScene()
{
yield return new WaitForSeconds(1.5f);
SceneManager.LoadScene(sceneName);
}
}
It looks like you didn't reference YouWin in your Manager script. You should include it by adding it as a global variable public YouWin <variable_name>; or by referencing your empty GameObject and getting the YouWin component:
public GameObject emptyObject;
emptyObject.GetComponent<YouWin>().picturesInPlace++;
After all thanks for helping me but I was able to find the right solution
just make in the first Script (Manager) :
bool static Done;
void Start()
{
bool Done = false;
}
public void OnEndDrag(PointerEventData eventData)
{
float distance = Vector3.Distance(transform.position, pos2.transform.position);
if (distance < 50)
{
transform.position = pos2.transform.position;
transform.localScale = pos2.transform.localScale;
canMove = false;
bool Done = True;
}
}
and just call it from the second Script (YouWin)
public string sceneName;
void Update()
{
if(Youwin.Done)
{
StartCoroutine(LoadScene());
}
}
IEnumerator LoadScene()
{
yield return new WaitForSeconds(0.5f);
SceneManager.LoadScene(sceneName);
}
This IS The right solution ;
The [SyncVar] attribute is not working for me in my game. I have made sure that:
I changed the variable with a command function
I correctly added the syncvar attribute and the hook
The client can update the variables on the server, but the server is not updating the variable on the client
Here are my scripts:
Player Shooting Script:
using UnityEngine;
using System.Collections;
using UnityEngine.Networking;
public class PlayerShoot : NetworkBehaviour {
public GameObject shootPosition;
public float shootRange = 100;
public float shootRate = 0.2f;
float nextCheck;
public int damage = 10;
// Use this for initialization
void Start () {
}
void DetectShooting(){
if (Time.time > nextCheck && Input.GetMouseButton(0)) {
nextCheck = Time.time + shootRate;
CmdShoot ();
}
}
[Command]
void CmdShoot(){
RaycastHit hit;
Ray bulletDirection = new Ray (shootPosition.transform.position, transform.forward * shootRange);
Debug.DrawRay (shootPosition.transform.position, transform.forward * shootRange, Color.blue, 10.0f);
if (Physics.Raycast (bulletDirection, out hit, 100)) {
print (hit.transform.name);
if (hit.transform.CompareTag ("Player")) {
hit.transform.GetComponent<PlayerHealth> ().DeductHealth (damage);
}
}
}
// Update is called once per frame
void Update () {
print (isLocalPlayer);
if (isLocalPlayer)
DetectShooting ();
}
}
Player Health Script:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using UnityEngine.Networking;
public class PlayerHealth : NetworkBehaviour {
public static int maxHealth = 100;
[SyncVar (hook ="UpdateUI")]
public int currentHealth = maxHealth;
public Slider healthBar;
void UpdateUI(int hp){
healthBar.value = currentHealth;
}
public void DeductHealth(int damage){
if (isServer)
currentHealth -= damage;
}
// Use this for initialization
void Start () {
//InvokeRepeating ("DeductHealth", 0, 2);
SetInitialReferences ();
}
void SetInitialReferences(){
}
// Update is called once per frame
void Update () {
}
}
Here are some Screen Shots:
Since you are hooking the SyncVar with function, you need to pass the variable manually (and do other stuff you wish to do with the new value like checking if hp <= 0).
void UpdateUI(int hp){
currentHealth = hp;
healthBar.value = currentHealth;
}