'Issue with OnCollisionEnter2D - No error messages but no result
first time posting here so apologies if I get anything wrong. I've written a chunk of code meant to check if the attached object is touching another object with the tag "hazard". If it is then the value of the float "hp" will be reduced by 1. No error messages are present when I run my game but when I run my game and touch an object tagged "hazard" with my player character hp isn't effected - I know this because I successfully implemented a health bar which doesn't change at all when I touch an object with the tag.
Below is the code I have written to detect the collision and reduce the value of hp.
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag =="hazard")
{
hp = hp - 1;
}
}
Both the player character object and hazard objects have 2D Collider components attached.
Upon request the code for the health has been added below:
This code sets the initial value of "hp" and declares the game objects that I later use to represent the remaining health of the player.
float hp = 10;
public GameObject phealth1;
public GameObject phealth2;
public GameObject phealth3;
public GameObject phealth4;
public GameObject phealth5;
public GameObject phealth6;
public GameObject phealth7;
public GameObject phealth8;
public GameObject phealth9;
public GameObject phealth10;
This code prevents "hp" from going above or below the maximum and minimum values as well as updating the health bar based on the value of "hp".
void Update()
{
if (hp > 10)
hp = 10;
if (hp < 0)
hp = 0;
switch (hp) {
case 10:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(true);
phealth10.gameObject.SetActive(true);
break;
case 9:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(true);
phealth10.gameObject.SetActive(false);
break;
case 8:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 7:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 6:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 5:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 4:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 3:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 2:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 1:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(false);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 0:
phealth1.gameObject.SetActive(false);
phealth2.gameObject.SetActive(false);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
}
}
Below is the entire script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Health : MonoBehaviour
{
float hp = 10;
public GameObject phealth1;
public GameObject phealth2;
public GameObject phealth3;
public GameObject phealth4;
public GameObject phealth5;
public GameObject phealth6;
public GameObject phealth7;
public GameObject phealth8;
public GameObject phealth9;
public GameObject phealth10;
// Start is called before the first frame update
void Start()
{
}
void dmg(int x)
{
hp = hp - x;
}
void OnCollisionEnter2D(Collision2D col)
{
if (col.gameObject.tag == "hazard")
dmg(1);
Debug.Log("Contact made");
Debug.Log(hp);
}
// Update is called once per frame
void Update()
{
if (hp > 10)
hp = 10;
if (hp < 0)
hp = 0;
switch (hp) {
case 10:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(true);
phealth10.gameObject.SetActive(true);
break;
case 9:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(true);
phealth10.gameObject.SetActive(false);
break;
case 8:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(true);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 7:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(true);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 6:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(true);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 5:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(true);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 4:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(true);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 3:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(true);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 2:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(true);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 1:
phealth1.gameObject.SetActive(true);
phealth2.gameObject.SetActive(false);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
case 0:
phealth1.gameObject.SetActive(false);
phealth2.gameObject.SetActive(false);
phealth3.gameObject.SetActive(false);
phealth4.gameObject.SetActive(false);
phealth5.gameObject.SetActive(false);
phealth6.gameObject.SetActive(false);
phealth7.gameObject.SetActive(false);
phealth8.gameObject.SetActive(false);
phealth9.gameObject.SetActive(false);
phealth10.gameObject.SetActive(false);
break;
}
}
}
Solution 1:[1]
I suspect something is wrong in the scene.
- Check that there is only one of this script in the scene.
In the
Hierarchy Tabtype in the search bart:MyComponentName. Multiple copies using the same phealth objects could cause the issue you are experiencing.
- Double check your
phealth{n}gameObjects that are assigned in the inspector.
Here is a cleaned up version of your code. Tested and confirmed working.
using UnityEngine;
public class Health : MonoBehaviour
{
public int health;
public int maxHealth;
public GameObject[] healthObjs;
private void OnCollisionEnter2D(Collision2D collision)
{
if (collision.gameObject.CompareTag("hazard"))
{
AddHealth(-1);
}
}
private void AddHealth(int amount)
{
health = Mathf.Max(0, Mathf.Min(maxHealth, health + amount));
UpdateDisplayedHealth();
}
private void UpdateDisplayedHealth()
{
for (int i = 0; i < healthObjs.Length; i++)
{
healthObjs[i].SetActive(i + 1 <= health);
}
}
}
- Removed the switch case replacing it with a for loop which makes the code much more compact.
- Updated the damage function to handle the min/max health value and to update the displayed health.
- Removed Update completely.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | hijinxbassist |
