AddListener fuction throwing a NullReferenceException error
Been trying to create a little shop canvas/menu that you can click 3 buttons to upgrade the player shooting script in the Survival Shooter Game.
[code]
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI;
public class Shop : MonoBehaviour {
public GameObject shopCanvas;
public Button upDamage;
public Button upROF;
public Button upRange;
// Use this for initialization
void Start () {
Button upDamage = gameObject.GetComponent<Button> ();
Button upROF = gameObject.GetComponent<Button> ();
Button upRange = gameObject.GetComponent<Button> ();
upDamage.onClick.AddListener (PurchaseUpgrade);
upROF.onClick.AddListener (PurchaseUpgrade); **ERRORS ARE HERE!**
upRange.onClick.AddListener (PurchaseUpgrade);
}
// Update is called once per frame
void Update () {
}
void PurchaseUpgrade() {
GameObject.FindGameObjectWithTag ("Player");
gameObject.GetComponentInChildren<PlayerShooting> ();
if (upDamage) {
gameObject.GetComponentInChildren<PlayerShooting> ()
.damagePerShot = 20;
}
if (upROF) {
gameObject.GetComponentInChildren<PlayerShooting> ()
.timeBetweenBullets = 0.1f;
}
if (upRange) {
gameObject.GetComponentInChildren<PlayerShooting> ()
.range = 200.0f;
}
Debug.Log("Success");
}
}
[/code]
What I am trying to do is make these buttons clickableand upgrade the right parameters in my game, but for some reason the add listener keeps giving me a NullReferenceException error. I am very new to C# (Just started learning it a few days ago).
Well, firstly, get rid of these:
Button upDamage = gameObject.GetComponent<Button> ();
Button upROF = gameObject.GetComponent<Button> ();
Button upRange = gameObject.GetComponent<Button> ();
and link your fields in the Inspector ins$$anonymous$$d.
GetComponent only works if you have more than one component of that type on the gameobject. If you have more than one Button, there's no way for GetComponent to tell which one you mean. I don't know what happens if you use GetComponent like this, and it may or may not be causing your error, but it's definitely going to cause SO$$anonymous$$E kind of problem.
Got rid of those, so hopefully that fixed something. And what do you mean by link fields to the Inspector?
When you declare a public field in your script (like public Button upDamage;
, it creates a field in the inspector for your script component. You can drag a Button component to that field, and Unity will assign that Button component to your Button field when the game initializes.
It does the same thing as using this.upDamage = this.GetComponent<Button>()
, except you can specify exactly which button you're using, so it works even if you have multiple button components attached (you can also attach components from other objects this way).
$$anonymous$$ore info on this subject here (whole page) and here (toward the bottom).
Answer by IamShadow · Mar 18, 2017 at 03:18 AM
@Commoble - its not letting me reply to your message but, I removed getcomponentinchildren, and I am not getting any errors anymore. However it's still not changing any of the actual parameters when I click the buttons, not sure why..
I just noticed you're doing if (upDamage)
. Treating a button (or any UnityEngine object) like a bool returns true if the object exists and false if it's null, which I doubt is what you're intending to use that if statement for.
Ins$$anonymous$$d of assigning the same listener function to each button and then trying to figure out what button it is, you'll have a lot fewer headaches if you assign a separate function to each button, so the functions don't have to deter$$anonymous$$e which button called them.
Hello! I finally managed to get it to work thanks to you! really appreciate the help!
void PurchaseUpgrade1() {
if (upDamage && gunBarrel != null) {
gunBarrel.GetComponent<PlayerShooting> ().damagePerShot = 50;
}
I made a public GameObject that takes in a gunBarrel which is the object that has the shooter script on it.