I have a game where the player controls a ball, trying to dodge falling cubes. He has three lives, and when he touches a cube, one of the lives are gone… This worked fine and all, until I tried to implement an invincibility powerup that when collides with the player trigger, will temporarily disable the collider using “collider.enabled = false”. Now this was planned out, and I know the error exists there but I can’t seem to find it. The Collider seems to disable as soon as the project runs and I tried figuring out why. I tried debugging when the invincibility powerup collides with the player, but it does not debug, given the fact that the collider is disabled as soon as the project launches.
Here are my three main scripts:
PLAYER MOVEMENT:
using UnityEngine;
using System.Collections;
public class Move : MonoBehaviour {
public static bool playerAlive = true;
public int lives = 3;
public GameObject life1;
public GameObject life2;
public GameObject life3;
// Use this for initialization
void Start () {
this.gameObject.collider.enabled = true;
}
// Update is called once per frame
void Update () {
transform.position += (new Vector3(Input.GetAxis("Horizontal"), 0, 0) / 5);
if(lives == 2) {
Destroy(life1.gameObject);
}
if(lives == 1) {
Destroy(life2.gameObject);
}
if(lives == 0) {
Destroy(life3.gameObject);
}
if(Invinc.hasInvinc = true) {
this.gameObject.collider.enabled = false;
}
if(Invinc.hasInvinc = false) {
this.gameObject.collider.enabled = true;
}
}
void OnTriggerEnter(Collider items) {
if(items.gameObject.tag == "EvilCube") {
lives--;
if(lives < 0) {
Destroy(gameObject);
playerAlive = false;
}
}
}
}
INVINCIBILITY BEHAVIOR:
using UnityEngine;
using System.Collections;
public class Invinc : MonoBehaviour {
public static bool hasInvinc = false;
public static int timeLoad = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
if(hasInvinc = true) {
if(timeLoad < 750) {
timeLoad++;
}
if(timeLoad > 750) {
hasInvinc = false;
}
}
}
}
INVINCIBILITY POWERUP (the one applied to the powerup object itself):
using UnityEngine;
using System.Collections;
public class InvincPower : MonoBehaviour {
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
}
void OnTriggerEnter(Collider item) {
if(item.gameObject.tag == "Invincibility") {
Invinc.hasInvinc = true;
Debug.Log("Invincibility Initiated!!!!!!!!!!!!!!");
}
}
}