• 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
Question by MegaBlueBerry · Apr 04, 2020 at 06:14 AM · 2dinstantiateshootingplatformer

My shooting system wont work, help

I am making a 2D platformer shooter game. After coding for an hour or so I tried it out but to my disappointment, it gave an error. I have now been trying to fix it for an hour but has stumped me.

I get these two errors

 NullReferenceException: Object reference not set to an instance of an object
 ShootingScript.Shoot (BulletInfo bullet) (at Assets/Scripts/ShootingScript.cs:111)
 ShootingScript.Update () (at Assets/Scripts/ShootingScript.cs:40)

 NullReferenceException: Object reference not set to an instance of an object
 ShootingScript.Start () (at Assets/Scripts/ShootingScript.cs:25)

This is my script

 using System.Collections;
 using System.Collections.Generic;
 using UnityEngine;
 
 public class ShootingScript : MonoBehaviour
 {
     [SerializeField]
     public WeaponList[] weaponLists;
 
     WeaponScriptableObj currentWeapon;
     BulletInfo bullet1;
     BulletInfo bullet2;
 
     int currentAmmo;
     int maxAmmo;
 
     int oldCurrentAmmo;
     int oldWeaponSlot;
 
     public GameObject blankBullet;
     public GameObject firePoint;
 
     void Start()
     {
         bullet1 = currentWeapon.regularBullets; <-----Line 25
         bullet2 = currentWeapon.specialBullets;
 
         ChangeWeapon(1);
     }
 
     void Update()
     {
         //Firing input
         if (Input.GetButton("Fire1") && currentAmmo >= 1)
         {
             Shoot(bullet1);
         }
         if (Input.GetButtonDown("Fire2") && currentAmmo >= 2)
         {
             Shoot(bullet2); < -------- Line 40
         }
       
     public void Shoot(BulletInfo bullet)
     {
         //Shooting logic
         GameObject currentShotBullet = Instantiate(blankBullet, firePoint.transform.position, firePoint.transform.rotation);
         currentShotBullet.GetComponent<BlankBullet>().FireBullet(bullet.bulletSpeed, bullet.damage, bullet.range, bullet.bulletSprite); <------ Line 111
     }
 }













Comment

People who like this

0 Show 0
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

  • Sort: 
avatar image
Best Answer

Answer by n_rusev · Apr 04, 2020 at 06:32 AM

You never initialize your WeaponScriptableObj currentWeapon; , so when you first reference it in Line 25 you get an error, since this object is null, which means your bullet1 and bullet2 are null as well - error at line 40. At line 111 your BulletInfo bullet parameter is probably null again. To fix this either make WeaponScriptableObj currentWeapon public and set it to something in the editor, or initialize it in your start method before trying to initialize bullet1 and bullet2.

Comment
MegaBlueBerry

People who like this

1 Show 1 · 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 MegaBlueBerry · Apr 04, 2020 at 07:20 AM 0
Share

Switching the order of my start method made it work perfectly, Thank you

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

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

Related Questions

Why isn't my instance of an object moving? 1 Answer

I cant get my 2d shooting script to work? 2 Answers

How do I instantiate in front of my character? 1 Answer

I don't know wat is wrong with this code? 2 Answers

My Unity 2D platoformer shooter question. 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