Using 'Instantiate' but I can't see in Game Tab, Help!!

The player should shot the bullet object.

So, PlayerBullet prefab, ShotPoint, Player were inserted in the Player Object Inspector.

  1. PlayerBullet

  2. ShotPoint

  3. Player has Script

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

[System.Serializable]
public class Boundary
{
    public float xMin, xMax, yMin, yMax;  // The limit values of Player's moves(forward,back,left,right)
}

public class Movement : MonoBehaviour
{
    public float moveSpeed;    // Player move speed
    public Boundary boundary;  // Objective variable for Boundary
    public Rigidbody2D rb;     // Objective variable for Rigidbody2D

    public GameObject shot;         // shot prefeb
    public Transform ShotPoint;     // shot start
    public float fireRate = 0.5f;   // shot term
    private float nextFire = 0.0f;

    // Use this for initialization
    void Start()
    {

    }

    // Update is called once per frame
    void Update()
    {
        // Set the Player's Limited move sections
        GetComponent<Transform>().position = new Vector2(Mathf.Clamp(GetComponent<Transform>().position.x, boundary.xMin, boundary.xMax), Mathf.Clamp(GetComponent<Transform>().position.y, boundary.yMin, boundary.yMax));

        // Set the Player's movements options in this game, Just Left and Right
        // Right
        if (Input.GetKeyDown(KeyCode.RightArrow))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(moveSpeed, 0);
        }
        // Left
        if(Input.GetKeyDown(KeyCode.LeftArrow))
        {
            GetComponent<Rigidbody2D>().velocity = new Vector2(-moveSpeed, 0);
        }

        nextFire = Time.time + fireRate;
        Instantiate(shot, ShotPoint.position, ShotPoint.rotation);
    }
}
  1. PlayerBullet has Script
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ShotPlayer : MonoBehaviour
{

    public float speedBullet;

    // Use this for initialization
    void Start()
    {
        GetComponent<Rigidbody2D>().velocity = transform.up * speedBullet;
    }

    // Update is called once per frame
    void Update()
    {

    }
}
  1. But, I can’t see the PlayerBullet in the Game!!

    I think everything is OK
    What is the problem??
    I want to know why.

Help me, please.

Change your “Order in layer” to a bigger number in bullet prefab and try again

Also, put in a Debug.Break() call when you spawn the bullet, then go looking through your scene to see if the bullet is present.

To help gain more insight into your problem, I recommend liberally sprinkling Debug.Log() statements through your code to display information in realtime.

Doing this should help you answer these types of questions:

  • is this code even running? which parts are running? how often does it run? what order does it run in?
  • what are the values of the variables involved? Are they initialized? Are the values reasonable?

Knowing this information will help you reason about the behavior you are seeing.

You can also put in Debug.Break() to pause the Editor when certain interesting pieces of code run, and then study the scene

You could also just display various important quantities in UI Text elements to watch them change as you play the game.

If you are running a mobile device you can also view the console output. Google for how on your particular mobile target.

Here’s an example of putting in a laser-focused Debug.Log() and how that can save you a TON of time wallowing around speculating what might be going wrong:

Also if you’re using physics and spawn the bullet “inside” the player, it could be getting ejected at massive velocities and flying far away before you see it.

If this is happening, one approach is to use the physics system layers to specify the player layer and bullet layers should not collide.

There is a message :


UnassignedReferenceException: The variable Shot of Movement has not been assigned.
You probably need to assign the Shot variable of the Movement script in the inspector.

Sorry, Help.

Oh dear… you do not need to post to the forum for that!!!

Why?

Because the answer is always the same… ALWAYS. It is the single most common error ever.

Don’t waste your life spinning around and round on this error. Instead, learn how to fix it fast… it’s EASY!!

Some notes on how to fix a NullReferenceException error in Unity3D

  • also known as: Unassigned Reference Exception
  • also known as: Missing Reference Exception
  • also known as: Object reference not set to an instance of an object

http://plbm.com/?p=221

The basic steps outlined above are:

  • Identify what is null
  • Identify why it is null
  • Fix that.

Expect to see this error a LOT. It’s easily the most common thing to do when working. Learn how to fix it rapidly. It’s easy. See the above link for more tips.

This is the kind of mindset and thinking process you need to bring to this problem:

https://discussions.unity.com/t/814091/4

Step by step, break it down, find the problem.

Here is a clean analogy of the actual underlying problem of a null reference exception:

https://discussions.unity.com/t/840647/7