0

I am working on a Unity2D game with a class that is used as a template (I know that's not the term) for a gun system. When the game starts, a function is called to show the gun's thumbnail in a UI.Image element. But when I go to Unity to start the game, it gives me the compiler editor NullReferenceException: Object reference not set to an instance of an object PlayerScript.Start () (at Assets/Scripts/PlayerScript.cs:19). Here is the relevant portion of PlayerScript.cs:

using System.Collections.Generic;
using UnityEngine;

public class PlayerScript : MonoBehaviour
{

    [SerializeField] static KeyCode fireKey;
    [SerializeField] static GameObject gunGameObject;
    [SerializeField] static GunObject currentGun;
    [SerializeField] static GunObject starterGun;

    private float movementSpeed = 5f;

    // Start is called before the first frame update
    void Start() {
        currentGun = starterGun;
        Debug.Log("currentGun is " + currentGun);
        currentGun.switchGun();
    } 

Here is the template file from which it is calling switchGun() (GunObject.cs)

using System.Collections.Generic;
using UnityEngine;

[CreateAssetMenu(menuName = "Gun")]

public class GunObject : ScriptableObject {
    [SerializeField] string gunName;
    [SerializeField] public Sprite uiImage;
    [SerializeField] GunObject nextUpgrade;
    [SerializeField] int damage;
    [SerializeField] int shotsPerSecond;
    [SerializeField] int ammoCapacity;
    [SerializeField] float bulletSpeed;

    public void switchGun() {
        Game game = new Game();
        game.gunDisplay.sprite = uiImage;
    }

Thank you for your time. If you need any more information, please comment what you need.

  • The error doesn't sound like coming from `switchGun` but rather already from `PlayerScript.Start`! Make sure `starterGun` is referenced correctly ... In general: **Start [Debugging](https://docs.unity3d.com/Manual/ManagedCodeDebugging.html) your code!** – derHugo Mar 02 '20 at 05:46
  • I don't think I can use this article because I can't do so much as run the game, it's a compiler error – wiggleforlife Mar 02 '20 at 05:50
  • No it is **not**! A `NullReferenceException` is **never** a compiler but rather a runtime error. If there was any compiler error you couldn't even enter the play mode in Unity btw ... – derHugo Mar 02 '20 at 05:52
  • And I can't enter the play mode in Unity. If I try it says to fix all the compiler errors first. – wiggleforlife Mar 02 '20 at 05:53
  • Well .. so what are the compiler errors? You clearly didn't show this here in your question? ^^ what is `Game`? If this is of type `MonoBehaviour` then your mistake lies already here ... `new` is absolutely forbidden for `MonoBehaviour` classes ... you want to rather use `AddComponent` or `Instantiate` ... – derHugo Mar 02 '20 at 05:54
  • Ay I also ask why all your fields in `PlayerScript` are `static`? Remove this you shouldn't need them `static`! Note that `static` fields are not serialized/saved so back to my first comment: `starterGun` will surely be `null`. – derHugo Mar 02 '20 at 05:57
  • After I removed the ```static``` bits, I get the error ```An object reference is required for the non-static field, method, or property 'Game.gunDisplay'```. I am not sure how to use AddComponent, as it just throws more errors. – wiggleforlife Mar 02 '20 at 06:00
  • Also, Game.cs literally consists of ```[SerializeField] public Image gunDisplay;``` – wiggleforlife Mar 02 '20 at 06:02
  • 2
    I'm sorry but without seeing your actual code and errors it is quite impossible to help here ... I suggest you take your time, put together all relevant code and information into a new question where you describe exactly one problem at a time. In particular include all your custom types and classes because otherwise it is not reproducible by anyone.. – derHugo Mar 02 '20 at 06:04

0 Answers0