Home > Blockchain >  The Object you want to instantiate is null. (Error Unity)
The Object you want to instantiate is null. (Error Unity)

Time:08-07

I've tried to fix this problem for many days, looked for every result in a web and didn't get any suitable answers to me...

With this problem I can run my game, but it shows up every time I shoot the bullet prefab.

This is a player shooting script, it shows up a problem when I try to Instatiate(bulletRef);

public GameObject bulletRef;

// Start is called before the first frame update
void Start()
{
    bulletRef = Resources.Load("Bullet") as GameObject;   
}

// Update is called once per frame
void Update()
{
    if (Input.GetKeyDown(KeyCode.Z))
    {
        // By pressing the button we create an instance of a bullet
        Instantiate(bulletRef);
        bulletRef.transform.position = new Vector3(transform.position.x   .4f, transform.position.y   .2f, -1);
    }
}

I think that those codes look suspicious to me, maybe the roots of this problem are coming out from them

here is a script for bullet

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

public class BulletScript : MonoBehaviour
{
    public float speed = 20f;
    public int damage = 1;
    public Rigidbody2D rb;

    // Start is called before the first frame update
    void Start()
    {
        rb.velocity = -transform.up * speed;
    }

    private void OnTriggerEnter2D(Collider2D collider)
    {
        Destruction enemy = collider.GetComponent<Destruction>();
        if (enemy != null)
        {
            enemy.TakeDamage(damage);
        }

        Destroy(gameObject);
    }
}

This is a script for destroying the bullet. I don't personally think that there is an issue with this code, but it is for you to understand how my bullet dissapears

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

public class Destruction : MonoBehaviour
{
    public int health = 1;

    public void TakeDamage(int damage)
    {
        health -= damage;

        if (health <= 0)
        {
            Die();
        }
    }

    void Die()
    {
        Destroy(this.gameObject);
    }

    private void OnTriggerEnter2D(Collider2D collision)
    {
        ScoreManager.instance.AddPoint();
    }
}

CodePudding user response:

The problem lies in your Update() method of your first script

Instantiate(bulletRef);
bulletRef.transform.position = new Vector3(transform.position.x   .4f, transform.position.y   .2f, -1);

You are instantiating the gameobject bulletRef but not assigning it to any field. Also, you are changing the position of just a reference to the gameobject you just loaded from the resources. You didn't have a reference to the gameobject in the scene, you have a reference to the gameobject in your resources. To get a reference to the gameobject you just instantiated into your scene, assign the instantiated gameobject to a field.

GameObject bulletRefObject = Instantiate(bulletRef);
bulletRefObject.transform.position = new Vector3(transform.position.x   .4f, transform.position.y   .2f, -1);

CodePudding user response:

When you instantiate an Object, a clone is added to your hierarchy. This object is different from your original prefab. You cannot change the transform of the original prefab as it doesn't exist in your Scene.

The best Solution is what Geeky Quentin gave you.

  • Related