Collider disabling by itself...
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!!!!!!!!!!!!!!");
}
}
}
Answer by ZaninDevelopper · Oct 25, 2016 at 01:34 AM
You probably never solved this issue, since there are no edits, so I'm just going to leave the answer here to whomever this concerns
You have 2 issues on your script "Move". More exactly on lines 36 - 42 :
if(Invinc.hasInvinc = true) {
this.gameObject.collider.enabled = false;
}
if(Invinc.hasInvinc = false) {
this.gameObject.collider.enabled = true;
}
You used
if( a = b )
where the correct is
if (a == b )
you're basically applying the value of b to a.
I tested and this does cause the collider to always be disabled. Also, you have the same problem in line 18 of the "Invic" script.
I had a problem like this once, where I used one "=" instead of 2 in an if... Took me around 2 hours to find the problem, so I know the hassle.
By the way, a good line to use that would avoid this problem and clean up your code is:
this.gameObject.collider.enabled = !Invinc.hasInvinc;
Answer by OperationDogBird · Sep 23, 2013 at 07:42 PM
Seems like it would be better to do a check from within the PlayerMovement script inside the OnTrigger enter. If invincible, dont take any action. This will make it much more simple for you later on, and also keeps all of your logic in one place.
EDIT Here is a merged version of your 3 scripts. There are some things i left out for simplicity and readability.
PlayerMovement
public static bool playerAlive = true;
public int lives = 3;
public float invincibilityTime = 3.5f;
private float invincibleCounter = 0f;
private bool invincible = false;
void Update()
{
if (invincible)
{
if (invincibleCounter >= 1f)
{
invincible = false;
invincibleCounter = 0f;
}
else invincibleCounter += Time.deltaTime / invincibilityTime;
}
}
void OnTriggerEnter(Collider items)
{
if (items.CompareTag("EvilCube"))
{
if (invincible)
{
//Do some cool shader action that shows we are invincible maybe?
}
else
{
lives--;
if (lives < 0)
{
Destroy(gameObject);
playerAlive = false;
}
}
}
else if (items.CompareTag("Invincibility"))
{
invincible = true;
Debug.Log("Invincibility Initiated!!!!!!!!!!!!!!");
}
}
So should I replace my current OnTriggerEnter method in the Player$$anonymous$$ovement script with this one? What should I remove in its place?
Thank you very much for your assistance!
I would merge your scripts so you only have 1 OnTriggerEnter Callback for the player. So in the Player$$anonymous$$ovement i would do the check for the invincibility object, as well as the evil cube. Then you can just keep a bool value for invincibility in the Player$$anonymous$$ovement script. Give me a few $$anonymous$$utes, ill work up an example merged version for you.
Your answer
Follow this Question
Related Questions
Unity Crashes When Clicking Play: Script Error? 1 Answer
Check collsion of objects in another script 1 Answer
How to get a real time audio flow and analyze it? 0 Answers
How do I not set a trigger to happen? 1 Answer
Input Command Issues 0 Answers