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 OwClownfish · Dec 21, 2014 at 09:53 AM · c#bug

NullReferenceException on object while earlier in script is does work

I have a script in which there is a ammo pickup which should give the player an extra ammoclip. But when i try to pick it up I get an NullReferenceException : Object reference not set to an instance of an object error. The excact same line is earlier in the script and then it does work This is the script where the shooting and reloading is handled( there is more but that does work fine), I get the error for the bold text around line 35 in the first script. The ammoclip does get picked up but nothing happens because of the error. I hope someone has an solution.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 
 public class Shoot2 : MonoBehaviour {
 
     // Rocket Prefab
     public static Shoot2 Instance;
     public GameObject rocketPrefab;
     public AudioClip impact;
     public AudioClip Reloadsound;
     float timer;
     //public float timeBetweenBullets = 0.15f;
     public int clips = 6;
     int count = 8;
     bool reloading = false;
     public Slider Ammoslider;
     public Text Clips; 
     //private float timeStamp = Time.time + 5;
     void Awake ()
     {
                 Instance = this;
         }
     void Start () {
                 Clips.text = "Clips: " + clips.ToString ();
         }
 
     void Update () {
 
                 if (!reloading && count > 0 && Input.GetButtonDown ("Fire1"))
                         shoot ();
                 Ammoslider.value = count;
         }
 
         //else geluid van leeg geweer klik
      public void AddClip (int amount) {
         clips += amount;
         **Clips.text = "Clips: " + clips.ToString ();**
 
         }
     //void playSound () {
                 
     //    }
     void shoot () {
                 GameObject g = (GameObject)Instantiate (rocketPrefab, transform.position, transform.parent.rotation);
                 float force = g.GetComponent<Rocket> ().speed;
                 g.rigidbody.AddForce (g.transform.forward * force);
                 audio.PlayOneShot (impact, 0.7F);
                 --count;
                 if (count <= 0 && clips >= 1) {
                         Reload ();
                 }
                 
         }
     void Reload()
     {
 
         reloading = true;
         StartCoroutine(Reloadanim());
         // Do reloading stuff.
 
     }
 
     IEnumerator Reloadanim() {
     
     
         audio.PlayOneShot (Reloadsound, 1.0F);
         reloading = true; // we are now reloading
         clips--; // take away a clip
         Clips.text = "Clips: " + clips.ToString();
         yield return new WaitForSeconds(3); // wait for set reload time
 
         reloading = false; // done reloading
         count = 8; // fill up the gun
 }
 
 }

This second script is where I call the AddClip function so it should add the ammoclip.

 using UnityEngine;
 using System.Collections;
 
 public class Ammobox : MonoBehaviour {
     public static Ammobox Instance;
     Shoot2 shoot2;
     GameObject player; 
     public int Clipsize = 1;
     // Use this for initialization
     void Awake ()
     {
         // Setting up the references.
         Instance = this;
         shoot2 = GetComponent <Shoot2> ();
         player = GameObject.FindGameObjectWithTag ("Player");
     }
     
     void Start () {
         
     }
     
     // Update is called once per frame
     void Update () {
         
     }
     void OnTriggerEnter (Collider other)
     {
         // If the entering collider is the player...
         if(other.gameObject.tag == "Player")
         {this.gameObject.SetActive(false);
             Shoot2.Instance.AddClip(Clipsize);
 
         }
     }
 }


[1]: /storage/temp/37346-screenshot_2.png

[2]: /storage/temp/37347-screenshot_3.png

Edit : Specified the line where the error occurs.

screenshot_3.png (34.7 kB)
screenshot_2.png (43.1 kB)
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 meat5000 ♦ · Dec 20, 2014 at 04:53 PM 1
Share

Please post the code rather than pictures.

Edit: Good Job

avatar image meat5000 ♦ · Dec 21, 2014 at 02:45 AM 0
Share

A NullRef occurs where there is an object expected but nothing is found. For example, if a clip is Destroyed when picked up then if you have a reference to that object, it will suddenly point to nothing. You can have another clip but there reference doesn't point to it.

You may need to mark line 47 in your codepaste. It may have gotten a little messy.

0 Replies

· Add your reply
  • Sort: 

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

3 People are following this question.

avatar image avatar image avatar image

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

Object Reference not Set to an Instance of an Object in C# (Closed) 1 Answer

Distribute terrain in zones 3 Answers

Multiple Cars not working 1 Answer

Syntax issue- only assignment, call, increment, decrement, and new object expressions can be used as statement 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