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 Alienguard · Dec 13, 2013 at 02:55 AM · nullreferenceexceptionreferencenullexception

Need Help With a NullReference Exception

Ok, I am following the unity 3D tutorials from the official website, however I coding my own stuff as I go rather than just copying the code from the videos. The ideas is the following. I have a script where I hold data as ammo, bombs and so on. I have a spaceship shooting perfectly, but I want the ammo to decrease so I am using GetComponent to access the script holding the data and therefore be able to decrease it, however when I am going to test the code, I get a nullreferenceexception and I can't figure out what it is.

 using UnityEngine;
 using System.Collections;
 
 public class Shooting : MonoBehaviour 
 {
     public GameObject laserPrefab;
     public Transform guns;
     private Inventory inventory;
     //private float laserSpeed = 50;
 
     // Use this for initialization
     void Awake() 
     {
         inventory = GetComponent<Inventory>();
     }
 
     void Start()
     {
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         shoot();
     }
 
     void shoot()
     {
         if(Input.GetKeyDown(KeyCode.Space))
         {
             Instantiate(laserPrefab, guns.position, guns.rotation);
             inventory.ammo.bullets--; //This is the problem
             Debug.Log(inventory.ammo.bullets);
         }
     }
 }

Any help will be appreciated, thank you very much for your time!

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 YoungDeveloper · Dec 13, 2013 at 02:57 AM

Is the Inventory script attached on the same gameobject this script is ? If yes, show us inventory script.

And it's actually better to have function that removes bullets on inventory itself, instead of tweaking it from others script.

 inventory.RemoveBullets();

But in Inventory script create function

 public void RemoveBullets(){
     ammo.bullets--;
 }
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 Alienguard · Dec 13, 2013 at 04:15 PM

Well. the Inventory script is attached to the parent game object. That is, I have a spaceship model with the Inventory attached to it. Then I created a child object inside the ship which are the gun points and I attached the shooting script to them. I like to be a clear as possible so don't mind me doing a little diagram.

ShipGameObject.

  Components
       -Moving_Script -->which works perfectly
       -Inventory_Script
  Child_Object_Guns
       Components
            -Shooting_Script

I will show you the inventory script anyway and I will try your suggestion in the meantime. Thank You!

 using UnityEngine;
 using System.Collections;
 
 public class Inventory : MonoBehaviour 
 {
     public class Ammo
     {
         public int bullets;
         public int missiles;
         public int bombs;
         public int health;
 
         public Ammo(int bull, int mss, int bmb, int hp)
         {
             bullets = bull;
             missiles = mss;
             bombs = bmb;
             health = hp;
         }
     }
 
     public Ammo ammo = new Ammo(200, 50, 10, 200);
     // Use this for initialization
     void Start () 
     {
 
     }
     
     // Update is called once per frame
     void Update () 
     {
     
     }
 }
 
Comment
Add comment · 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 Alienguard · Dec 13, 2013 at 08:33 PM 0
Share

I ended fixing it today. Here is what I did. using UnityEngine; using System.Collections;

 public class Shooting : $$anonymous$$onoBehaviour 
 {
     public GameObject laserPrefab;
     public GameObject ship; //created ship game object
     public Transform guns;
 
 
     private Inventory inventory;
     //private float laserSpeed = 50;
 
     // Use this for initialization
     void Awake() 
     {
         inventory = ship.gameObject.GetComponent<Inventory>();//initialized inventory to the ship gameobject, so that I could access what I wanted.
     }
 
     void Start()
     {
 
     }
     
     // Update is called once per frame
     void Update () 
     {
         shoot();
     }
 
     void shoot()
     {
         if(Input.Get$$anonymous$$eyDown($$anonymous$$eyCode.Space))
         {
             Instantiate(laserPrefab, guns.position, guns.rotation);
             inventory.bulletDecrement(); //works like a charm now
         }
     }
 }


I have to thank young developer for the tip of having the decrement function on the actual inventory and calling it from there. Thanks for the help!

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

Multiple Cars not working 1 Answer

NullReferenceException Error 6 Answers

NullReferenceException - With Network PlayerSpawn 0 Answers

referencing a component of a gameobject 1 Answer

NullReferenceException question. 2 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