Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 nbaylis · Mar 11, 2016 at 03:17 PM · 2dscripting problemerrorscript errorinstance

Why do I keep getting this error?

Hey everyone, I've been using unity for about 8 months now. I'm making a top down dungeon game with a few friends and we've been able to solve every problem except this one. When I pick up my item that is suppose to increase the damage of my weapon. I get this error.

NullReferenceException: Object reference not set to an instance of an object PowerUp.OnTriggerEnter2D (UnityEngine.Collider2D other) (at Assets/Scripts/PowerUp.cs:12)

PowerUp Script (C#);

 //PowerUp Script
 using UnityEngine;
 using System.Collections;
 
 public class PowerUp : MonoBehaviour {
 
     public int addDamage;
 
 
     void OnTriggerEnter2D (Collider2D other)
     {
         if (other.gameObject.tag == "Player") {
             other.gameObject.GetComponent<attackTrigger> ().PowerUpIncrease (addDamage); 
             Destroy (gameObject);
         }
     }
 }

My Attack Trigger/Damage Script (C#):

 using UnityEngine;
 using System.Collections;
 
 public class attackTrigger : MonoBehaviour 
 {
     // Sets how much damage we want to do to the enemy. 
 
     public int startDamage;
     public int currentDamage;
     public int maxDamage;
 
 
     void Start ()
     {
         //Sets Starting Damage
         currentDamage = startDamage;
     }
     void Update()
     {
         //Makes it so damage can't be maxed not over the intended amount
         if (currentDamage>maxDamage){
             currentDamage = maxDamage;
         }
     }
         public void OnTriggerEnter2D(Collider2D other)
         {
             // Checks if the tag is listed as 'Enemy'. 
             if (other.gameObject.tag == "Enemy") 
             {
             // Accesses the enemyDamage.cs script. 
             other.gameObject.GetComponent<enemyDamage> ().HurtEnemy (currentDamage);  
             }
         }
 
         //The called variable in the other class...
     public void PowerUpIncrease(int addDamage){
         //Adds additional damage to the current damage...
         currentDamage += addDamage;
     }
 }

Enemy Damage Script (C#):

 using UnityEngine;
 using System.Collections;
 
 public class enemyDamage : MonoBehaviour 
 {
     public int MaxHealth = 100; 
     public int CurrentHealth; 
 
     void Start ()
     {
         // At the start of our game, we want the current health of the enemy to equal the max health.
         CurrentHealth = MaxHealth; 
     }
 
     void Update ()
     {
         // If the enemy's current health is less than zero...
         if (CurrentHealth <= 0) 
         {
             // Remove it from the game. 
             Destroy (gameObject); 
         }
     }
 
     public void HurtEnemy(int currentDamage)
     {
         CurrentHealth -= currentDamage; 
     }
 
     public void SetMaxHealth()
     {
         CurrentHealth = MaxHealth; 
     }
 
 
 }



The goal that I am trying to reach is when I get my power up. For the damage to increase.

Comment
Add comment · Show 1
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 Lo0NuhtiK · Mar 11, 2016 at 03:27 PM 0
Share

Throw a debug in your powerup script to see if you're getting the attackTrigger component.

          if (other.gameObject.tag == "Player") {
             var atkTrig = other.gameObject.GetComponent<attackTrigger> ();
             if(atkTrig == null) Debug.LogWarning("No AttackTrigger Found");
             else atkTrig.PowerUpIncrease(addDamage); 
              Destroy (gameObject);
          }


It may be that your "Player" enters the trigger, but your "Player" isn't the collider that has the attackTrigger component. Is it a child of the player? Does the actual GameObject that has the attackTrigger component and the collider have the tag "Player"? Double-check these things.

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by nbaylis · Mar 11, 2016 at 04:08 PM

After rechecking my tags to verify, I realized that the object that has the attackTrigger component attached to it is neither child nor a parent. It is a projectile that I spawn in another script. The attackTrigger script is just to clarify the damage that the spawned object will deal. How would I call the script on the object?

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 Lo0NuhtiK · Mar 17, 2016 at 09:39 AM 0
Share

Have your projectile instantiation script (the gun) add the increased damage to the projectile, have the powerup change a damage value in the gun script.

eg :
PowerUp -> ontriggerEnter -> if player, get Gun script, Gun.extraDamage += PowerUp.damageIncrease


Gun -> shoot : instantiate Projectile -> Projectile.currentDamage += Gun.extraDamage

avatar image nbaylis Lo0NuhtiK · Mar 17, 2016 at 03:54 PM 0
Share

Thank you so much. It works now. How do I make your answer correct?

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

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

Referencing another script error/problem 0 Answers

Getting weird error message!?!?!? 1 Answer

error CS1525: Unexpected symbol `' 2 Answers

Object reference not set to instance for my timer and my text 0 Answers

error CS0103: The name `canvasGroup' does not exist in the current context 3 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