- Home /
Power up not working
Hello I'm making a game like "ballz", for the first time I'm making powerUps in a game so I don't have a brief background about it, so what I need is when the ball hit a brick if the brick health is 5 and the powerUp takes from the health 5 so in one hit it will destroy the brick!, I tried a lot to make it work for me, I tried a lot to make it work but no result. Here in this code when I debug the game it's giving me true which is the powerUp it's working but it's not taking 5 from the health of the brick. SOMEBODY HELP PLEASE!
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class powerUp: MonoBehaviour {
public bool powerUp = false;
private BrickHealthManager HpManager;
void Start()
{
powerUp = true;
HpManager = FindObjectOfType<BrickHealthManager>();
}
private void OnTriggerEnter2D(Collider2D other)
{
if (other.gameObject.tag == "Ball")
{
Debug.Log(powerUp);
powerUp = true;
this.gameObject.SetActive(false);
}
}
void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.tag == "Ball")
{
TakeDamage (5);
}
}
}
BrickHealthManager script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class BrickHealthManager : MonoBehaviour {
public GameObject brickDestroy;
public int brickHealth;
private Text brickHealthText;
private GameManager gameManager;
private ScoreManager score;
private SkillCoin skillCoin;
// Use this for initialization
void Start ()
{
skillCoin = GetComponent<SkillCoin>();
score = FindObjectOfType<ScoreManager>();
gameManager = FindObjectOfType<GameManager> ();
brickHealth = gameManager.level;
brickHealthText = GetComponentInChildren<Text> ();
}
void OnEnable()
{
gameManager = FindObjectOfType<GameManager> ();
brickHealth = gameManager.level;
}
// Update is called once per frame
void Update ()
{
brickHealthText.text = "" + brickHealth;
if (brickHealth <= 0) {
//Destroy Brick
score.IncreaseScore();
this.gameObject.SetActive(false);
Instantiate(brickDestroy, transform.position, Quaternion.identity);
}
}
public void TakeDamage(int damageToTake)
{
brickHealth -= damageToTake;
}
void OnCollisionExit2D(Collision2D other)
{
if (other.gameObject.tag == "Ball" || other.gameObject.tag == "Extra Ball")
{
TakeDamage (1);
}
}
}
Answer by shadowpuppet · Apr 15, 2018 at 07:52 PM
maybe because you are setting the gameObject.SetActive (false) on triggerEnter so it doesn't execute the OnTriggerExit function where the brick takes damage? slip in Debug.Log ("TriggerExit"); in there to see if it is even being called.
So, I should put the gameObject.SetActive(false) on OnTriggerExit function?
maybe put the TakeDamage (5); call just before the this.gameObject.SetActive(false); and eli$$anonymous$$ate the OnTriggerExit altogether. but I would still put in that Debug line to see if it is being called or not first
I put it in the OnTriggerExit, it didn't give me true or false and even its not collecting the power up!
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Horizontal Input showing random decimal when not pressing anything 0 Answers
[C#] Both button check with delay. 1 Answer