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 /
avatar image
0
Question by Ilkzz · Apr 10, 2014 at 05:25 PM · 2dyieldpower up

Invincibility Power Up

Hi guys,

I am trying to create a invincibility powerup that lasts a few seconds

I have the following code:

 private PlayerHealth healthScript;
 
     void Awake () 
     {
         healthScript = transform.root.GetComponent<PlayerHealth>();
         
     }
 
     // Use this for initialization
     void OnCollisionEnter2D (Collision2D other) 
     {
         if (other.gameObject.tag == "Player")
         {    
             StartCoroutine(NoDamage());
             GetComponent<SpriteRenderer>().enabled = false;
             GetComponent<BoxCollider2D>().enabled = false;
         }
     }
 
     IEnumerator NoDamage () 
     {
         Debug.Log ("Health Disabled " + PlayerHealth.health);
         healthScript.enabled = false;
         yield return new WaitForSeconds(5);
         Debug.Log ("Health Enabled " + PlayerHealth.health);
 
         healthScript.enabled = true;
         //Destroy(gameObject);
     
     }

and I receive an error saying "object reference not set to an instance of an object" on the line "healthScript.enabled = false;"

This script is on the character.

Why does it tell me the script doesn't exist? Am I doing it wrong? What other way can I go about it?

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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by _met44 · Apr 10, 2014 at 05:30 PM

It seems that your healthScript variable isn't set properly. Also, if I understand it right you place this script on the power up and not on the player, so the healthScript = transform.root.GetComponent(); will return null because the script isn't on the same object you are calling it from.

Try this:

 void OnCollisionEnter2D (Collision2D other) 
     {
        if (other.gameObject.tag == "Player")
        { 
          healthScript = other.gameObject.GetComponent<PlayerHealth>();
          StartCoroutine(NoDamage());
          GetComponent<SpriteRenderer>().enabled = false;
          GetComponent<BoxCollider2D>().enabled = false;
        }
     }
  
     IEnumerator NoDamage () 
     {
        healthScript.enabled = false;
        Debug.Log ("Health Disabled " + healthScript.health);
        yield return new WaitForSeconds(5);
        Debug.Log ("Health Enabled " + healthScript.health);
  
        healthScript.enabled = true;
        //Destroy(gameObject);
  
     }
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 Ilkzz · Apr 10, 2014 at 05:43 PM 0
Share

You are correct. $$anonymous$$y health script is on my character and my power up script is on my power up.

I have no error this time, but the script is still not disabled.

avatar image putlucky · Apr 10, 2014 at 09:37 PM 0
Share

Are you calling these methods from Update? That might be why it still wont disable.

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

23 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 avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Increase variable for a few seconds 2 Answers

Assets/Scripts/PlayerController.cs(32,49): error CS0126: An object of a type convertible to `float' is required for the return statement 1 Answer

Different execution rates in IEnumerator 1 Answer

Temporary boost 1 Answer

2D Animation does not start 1 Answer


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