Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 14 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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 bradarnott · Dec 08, 2016 at 06:46 AM · coroutinegetcomponentontriggerenterienumeratorpowerup

Damager Powerup issue - NullReferenceException: Object reference not set to an instance of an object

Hi all, I am currently creating a damage powerup, so when my player collides with an object it will increase the damage essentially. I have used this method for other powerups but for some reason it is not linking this one.

My powerup code is

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class damagePowerup : MonoBehaviour {
 
     public int damageIncrease = 10;
 
     void Update () {
         gameObject.transform.Rotate (new Vector3(0, 30, 0) * Time.deltaTime);
     }
 
     public void OnTriggerEnter(Collider other) {
 
         if (other.tag == "Player") {
             Debug.Log("Damage Powerup");
             PlayerShooting NewDamage = other.gameObject.GetComponent<PlayerShooting> ();
             NewDamage.damagePerShot = damageIncrease;
             transform.position = new Vector3(10000, 10000, 100000);
             StartCoroutine ("WaitTime", NewDamage);
         }
     }
 
     public IEnumerator WaitTime(PlayerShooting playerDamage) {
         yield return new WaitForSeconds (5);
         playerDamage.damagePerShot = 100;
         Destroy (gameObject);
     }
 }
 

and my PlayerShooting script that I am accessing is

 using UnityEngine;
 
 public class PlayerShooting : MonoBehaviour
 {
     public int damagePerShot = 20;
     public float timeBetweenBullets = 0.15f;
     public float range = 100f;
 
 
     float timer;
     Ray shootRay;
     RaycastHit shootHit;
     int shootableMask;
     ParticleSystem gunParticles;
     LineRenderer gunLine;
     AudioSource gunAudio;
     Light gunLight;
     float effectsDisplayTime = 0.2f;
 
 
     void Awake ()
     {
         shootableMask = LayerMask.GetMask ("Shootable");
         gunParticles = GetComponent<ParticleSystem> ();
         gunLine = GetComponent <LineRenderer> ();
 //        gunAudio = GetComponent<AudioSource> ();
     }
 
 
     void Update ()
     {
         timer += Time.deltaTime;
 
         if(Input.GetButton ("Fire1") && timer >= timeBetweenBullets && Time.timeScale != 0)
         {
             Shoot ();
         }
 
         if(timer >= timeBetweenBullets * effectsDisplayTime)
         {
             DisableEffects ();
         }
     }
 
 
     public void DisableEffects ()
     {
         gunLine.enabled = false;
     }
 
 
     public void Shoot ()
     {
         timer = 0f;
 
 //        gunAudio.Play ();
         gunParticles.Play ();
 
 
         gunLine.enabled = true;
         gunLine.SetPosition (0, transform.position);
 
         shootRay.origin = transform.position;
         shootRay.direction = transform.forward;
 
         if(Physics.Raycast (shootRay, out shootHit, range, shootableMask))
         {
             EnemyHealth enemyHealth = shootHit.collider.GetComponent <EnemyHealth> ();
             if(enemyHealth != null)
             {
                 enemyHealth.TakeDamage (damagePerShot, shootHit.point);
             }
             gunLine.SetPosition (1, shootHit.point);
         }
         else
         {
             gunLine.SetPosition (1, shootRay.origin + shootRay.direction * range);
         }
     }
 
 }
 

The error I am getting when playing the game is NullReferenceException: Object reference not set to an instance of an object damagePowerup.OnTriggerEnter (UnityEngine.Collider other) (at Assets/Scripts/damagePowerup.cs:18)

I do not understand why it doesn't like this line of code as I am accessing the script, getting the variable I want and added a new value?

If someone could help me out that would be great!

Comment
Add comment · Show 2
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 roman_sedition · Dec 08, 2016 at 07:13 AM 1
Share

You are getting a reference error for line 18, which is probably related to line 17, there might not be a PlayerShooting component on other.gameObject

I think you will run into some behavioral problems when you fix this though, on line 18 you are assigning the damage variable to be 10 even though the default damage is 20, which would mean you do less damage on the powerup. In line 19, you make the powerup go way offscreen ins$$anonymous$$d of destroying it?

On line 26 of your coroutine, after 5 seconds pass the damage then goes from 10 to 100, so the damage doesn't go back to normal it just goes incredibly high.

avatar image bradarnott roman_sedition · Dec 08, 2016 at 09:58 AM 0
Share

Thankyou! You made it click in my brain that the playerShooting script was actually on a child element, plain sailing from there. The values were just random while testing, however all is good now, again, thankyou :)

2 Replies

· Add your reply
  • Sort: 
avatar image
0
Best Answer

Answer by bradarnott · Dec 08, 2016 at 10:05 AM

 PlayerShooting NewDamage = other.gameObject.GetComponentInChildren<PlayerShooting> ();

Was needed, due to the script being on a child element of the player

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 edcx · Dec 08, 2016 at 07:07 AM

It's actually telling you what the error is! All you need to do is look what those variables are at that line. What I can tell from here is that your "NewDamage" variable is null. And the reason it's being null is, the other object you are getting from trigger does not have "PlayerShooting" component.

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

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

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

How do Coroutines behave with Triggers? 1 Answer

Powerup issue - Speed 1 Answer

3rd person controller in c#? 1 Answer

making a 2D character Speak for different lenghts of time 2 Answers

Coroutine starts but doesn't run? 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