Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 /
  • Help Room /
avatar image
0
Question by IamShadow · Mar 18, 2017 at 01:16 AM · scripting problemuibuttoncanvasscript error

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).

Comment
Add comment · Show 5
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 Commoble · Mar 18, 2017 at 01:34 AM 0
Share

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.

avatar image IamShadow Commoble · Mar 18, 2017 at 01:46 AM 0
Share

Got rid of those, so hopefully that fixed something. And what do you mean by link fields to the Inspector?

avatar image Commoble IamShadow · Mar 18, 2017 at 02:08 AM 0
Share

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).

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image
0
Best Answer

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..

Comment
Add comment · Show 2 · 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 Commoble · Mar 18, 2017 at 03:29 AM 1
Share

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.

avatar image IamShadow Commoble · Mar 18, 2017 at 05:10 PM 0
Share

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.

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

158 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image

Related Questions

How to write a script for an existing button 1 Answer

Making UI block rays with touch inputs 0 Answers

SetActive(false) in Start does not seem to work 2 Answers

What have I done wrong on the OnClick script from Creating a Main Menu tutorial? 0 Answers

Problem with creating button via script. 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