Skip to main content
Variable name
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401
inGameAdBtn = GetComponent<Button>(); 

This line of code says "throw away whatever the designer assigned to the inGameAdButtoninGameAdBtn in the Inspector, and replace it with the Button component attached to this object.

...but there is no Button component attached to this object in your screenshot. So this throws away a perfectly good button and replaces it with null, the absence of a button.

Naturally when you next try to use this button, while holding only the absence of a button, you get an exception.

If you plan to wire up this reference in the Inspector, delete this line of code that tries to search for it on this object — it's not doing anything you want it to do.

inGameAdBtn = GetComponent<Button>(); 

This line of code says "throw away whatever the designer assigned to the inGameAdButton in the Inspector, and replace it with the Button component attached to this object.

...but there is no Button component attached to this object in your screenshot. So this throws away a perfectly good button and replaces it with null, the absence of a button.

Naturally when you next try to use this button, while holding only the absence of a button, you get an exception.

If you plan to wire up this reference in the Inspector, delete this line of code that tries to search for it on this object — it's not doing anything you want it to do.

inGameAdBtn = GetComponent<Button>(); 

This line of code says "throw away whatever the designer assigned to the inGameAdBtn in the Inspector, and replace it with the Button component attached to this object.

...but there is no Button component attached to this object in your screenshot. So this throws away a perfectly good button and replaces it with null, the absence of a button.

Naturally when you next try to use this button, while holding only the absence of a button, you get an exception.

If you plan to wire up this reference in the Inspector, delete this line of code that tries to search for it on this object — it's not doing anything you want it to do.

Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

inGameAdBtn = GetComponent<Button>(); 

This line of code says "throw away whatever the designer assigned to the inGameAdButton in the Inspector, and replace it with the Button component attached to this object.

...but there is no Button component attached to this object in your screenshot. So this throws away a perfectly good button and replaces it with null, the absence of a button.

Naturally when you next try to use this button, while holding only the absence of a button, you get an exception.

If you plan to wire up this reference in the Inspector, delete this line of code that tries to search for it on this object — it's not doing anything you want it to do.