Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by DubstepDragon · Sep 23, 2013 at 07:33 PM · c#gamepowerup

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!!!!!!!!!!!!!!");
         }
     }
     
 }
 
Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

2 Replies

· Add your reply
  • Sort: 
avatar image
1

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;
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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!!!!!!!!!!!!!!");
     }
 }
Comment
Add comment · Show 2 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DubstepDragon · Sep 23, 2013 at 08:09 PM 0
Share

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!

avatar image OperationDogBird · Sep 23, 2013 at 08:43 PM 0
Share

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

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

17 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges