Hi, I am currently working on my very first game and I have run into a problem that I can’t fix on my own. When it comes to writing scripts I am completely clueless, I used tutorials to get my game to the point it is at now. Basically I am working on a top down shooter game inspired by Hotline Miami and I want to make the player character pick up guns and use them. But every time I try to do that I am greeted by this error message
“NullReferenceException: Object reference not set to an instance of an object:
WeaponAttack.dropWeapon () (at assets/Scripts/WeaponAttack.cs:71)
WeaponAttack.Update () (at assets/Scripts/WeaponAttack.cs:34)” and nothing happens. I am really in need of help as I don’t know what to change in my script to make it work properly.
The script:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponAttack : MonoBehaviour {
GameObject bullet,curWeapon;
bool gun = false;
float timer = 0.1f, timerReset=0.1f;
PlayerAnimate pa;
SpriteContainer sc;
float weaponChange = 0.5f;
bool changingWeapon = false;
// Use this for initialization
void Start () {
pa = this.GetComponent<PlayerAnimate> ();
sc = GameObject.FindGameObjectWithTag ("GameController").GetComponent<SpriteContainer> ();
}
// Update is called once per frame
void Update () {
if (Input.GetMouseButton (0)) {
attack ();
}
if (Input.GetMouseButtonDown (0)) {
pa.resetCounter ();
}
if (Input.GetMouseButtonUp (0)) {
pa.resetCounter ();
}
if (Input.GetMouseButtonDown (1) && changingWeapon == false) {
dropWeapon ();
}
if (changingWeapon == true) {
weaponChange -= Time.deltaTime;
if (weaponChange <= 0) {
changingWeapon = false;
}
}
}
public void setWeapon(GameObject cur, string name, float fireRate, bool gun)
{
changingWeapon = true;
curWeapon = cur;
pa.setNewTorso (sc.getWeaponWalk (name), sc.getWeapon (name));
this.gun = gun;
timerReset = fireRate;
timer = timerReset;
}
public void attack()
{
pa.attack ();
/*if (gun == true) {
* pa.attack ();
* } else {
* //melee attack
pa.attack ();
}*/
}
public GameObject getCur()
{
return curWeapon;
}
public void dropWeapon ()
{
curWeapon.transform.position = this.transform.position;
curWeapon.SetActive (true);
setWeapon (null, "", 0.5f, false);
}
}
curWeapon isn’t assigned to anything is what it is telling you.
setWeapon is the only place you assign it, so unless that is being called from somewhere else, as far as your script is concerned, you have nothing to drop.
Well that is quite confusing, because I can’t see where else I could assign “curWeapon” to. I am tempted to delete the scripts and write them again, maybe I messed up somewhere and I can’t see it. Just out of curiosity, would you know where I should assign “curWeapon” to? I also have a WeaponPickup script if you are willing to help I can provide it. Maybe the issue lies in there. Here it is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class WeaponPickup : MonoBehaviour {
public string Objectname;
public float fireRate;
WeaponAttack wa;
public bool gun;
//public AudioClips sfx;
// Use this for initialization
void Start () {
wa = GameObject.FindGameObjectWithTag ("Player").GetComponent<WeaponAttack> ();
}
// Update is called once per frame
void Update () {
}
void OnTriggerStay2D(Collider2D coll) {
Debug.Log ("Collision");
if (coll.gameObject.tag == "Player" && Input.GetMouseButtonDown (1)) {
//code to add weapon to player
Debug.Log ("Player picked up:" + name);
if (wa.getCur () != null) {
wa.dropWeapon ();
}
wa.setWeapon (this.gameObject,name,fireRate,gun);
//Destroy (this.gameObject);
this.gameObject.SetActive (false);
}
}
}
Additionally, you’re going to want to make your dropWeapon method more robust.
Suppose you try to drop a weapon multiple times, even though you aren’t carrying any weapons. i.e. you keep spamming the mouse button to drop your weapon, causing this code to run multiple times:
if (Input.GetMouseButtonDown (1) && changingWeapon == false)
dropWeapon ();
To resolve this, I suggest checking if you actually have a weapon to drop, like so:
Well I tried that and I stopped getting the error but I still wasn’t able to pick up the weapon. Now I have re-written the scripts and now the attack animations don’t work anymore and I still can’t pick up the weapon. I may be a lost cause.