- Home /
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.
Please post the code rather than pictures.
Edit: Good Job
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.