Unity - Error: Object Reference not set to an instance of an Object - Instantiate "Spawn" I need help

Asked

Viewed 59 times

0

This is my first game and it runs on Unity but just when I use the shoot button of this error "Unity - Error: Object Reference not set to an instance of an Object - Instantiate". follows the code down below:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour {
    private SpriteRenderer mySpriteRenderer;
    public int damage = 40;
    public GameObject bullet;
    private Transform SpawnerB;
    public float fireRate = 0.3f;
    public float nextFire = 0.0f;

    // Start is called before the first frame update
    void Start () {
        mySpriteRenderer = GetComponent<SpriteRenderer> ();
        bullet = GameObject.FindWithTag ("bull");

    }

    // Update is called once per frame
    void Update () {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        transform.rotation = Quaternion.LookRotation (Vector3.forward, mousePos - transform.position);
        if (Input.GetButtonDown ("Fire1") && Time.time > nextFire) {
            nextFire = Time.time + fireRate;
            Instantiate (bullet, SpawnerB.position, transform.rotation, );
        }
    }

}

1 answer

0

Look friend, it seems to me that your Unity is not finding its object in Start, check if they are with the tag (careful not to confuse the tag with Layer), and I move with Unity for 3 years, and I leave the tip, even being in Start(), if the object is already in the scene, Better to put it in the hand is much lighter than you give a Findwithtag that ends up being heavy. Another tip is to avoid public variables, if you just want to see them in the Inspector. have to put them like this:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Gun : MonoBehaviour {
    SpriteRenderer mySpriteRenderer;
    [SerializeField]
    int damage = 40;
    [SerializeField]
    GameObject bullet;
    Transform SpawnerB;
    [SerializeField]
    float fireRate = 0.3f;
    [SerializeField]
    float nextFire = 0.0f;

    // Start is called before the first frame update
    void Start () {
        mySpriteRenderer = GetComponent<SpriteRenderer> ();
        bullet = GameObject.FindWithTag ("bull");

    }

    // Update is called once per frame
    void Update () {
        Vector3 mousePos = Camera.main.ScreenToWorldPoint (Input.mousePosition);
        transform.rotation = Quaternion.LookRotation (Vector3.forward, mousePos - transform.position);
        if (Input.GetButtonDown ("Fire1") && Time.time > nextFire) {
            nextFire = Time.time + fireRate;
            Instantiate (bullet, SpawnerB.position, transform.rotation, );
        }
    }

}

Private variables do not have the need to put the private in front of you, because by default the variables are private.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.