Skip to main content
Post Closed as "Duplicate" by CommunityBot, Vaillancourt
Formatting code, marking line that throws exception, more descriptive title, tags
Source Link
DMGregory
  • 140.8k
  • 23
  • 257
  • 401

Prefab instantiation gives NullReferenceException when translating instantiated prefab

I keep getting this error NullReferenceException: Object reference not set to an instance of an object moveBlock.Update () (at Assets/Scripts/moveBlock.cs:27) whenever i

NullReferenceException: Object reference not set to an instance of an object moveBlock.Update () (at Assets/Scripts/moveBlock.cs:27)

whenever I press Play in Game or when iI press Space while in play. I looked at other questions posted but it is not clear to me whastwhat the solution is, i understand i. I think I am missing a GetComponentGetComponent line, but I'm not sure.

using System.Collections;

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

public class moveBlock : MonoBehaviour {

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

public class moveBlock : MonoBehaviour
{

    public float speed = 10;
    public GameObject[] blocks;
    private GameObject b;

    void Update()
    {
        Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
        Vector3 direction = input.normalized;
        Vector3 velocity = direction * speed;
        Vector3 moveAmount = velocity * Time.deltaTime;
        
        if (Input.GetKeyDown(KeyCode.Space))
        {
            b = (GameObject)Instantiate(blocks[Random.Range(0, 7)], new Vector3(0, 6, 0), Quaternion.Euler(Vector3.right));
            //b.AddComponent<Rigidbody>().isKinematic = false;
        }
        b.transform.Translate(moveAmount); // <-- Line 27 throws the exception.
    }
}

}

enter image description here

Prefab instantiation gives NullReferenceException

I keep getting this error NullReferenceException: Object reference not set to an instance of an object moveBlock.Update () (at Assets/Scripts/moveBlock.cs:27) whenever i press Play in Game or when i press Space while in play. I looked at other questions posted but it is not clear to me whast the solution is, i understand i am missing a GetComponent line but not sure.

using System.Collections;

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

public class moveBlock : MonoBehaviour {

public float speed = 10;
public GameObject[] blocks;
private GameObject b;

void Update()
{
    Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    Vector3 direction = input.normalized;
    Vector3 velocity = direction * speed;
    Vector3 moveAmount = velocity * Time.deltaTime;
    
    if (Input.GetKeyDown(KeyCode.Space))
    {
        b = (GameObject)Instantiate(blocks[Random.Range(0, 7)], new Vector3(0, 6, 0), Quaternion.Euler(Vector3.right));
        //b.AddComponent<Rigidbody>().isKinematic = false;
    }
    b.transform.Translate(moveAmount);
}

}

enter image description here

NullReferenceException when translating instantiated prefab

I keep getting this error:

NullReferenceException: Object reference not set to an instance of an object moveBlock.Update () (at Assets/Scripts/moveBlock.cs:27)

whenever I press Play in Game or when I press Space while in play. I looked at other questions posted but it is not clear to me what the solution is. I think I am missing a GetComponent line, but I'm not sure.

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

public class moveBlock : MonoBehaviour
{

    public float speed = 10;
    public GameObject[] blocks;
    private GameObject b;

    void Update()
    {
        Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
        Vector3 direction = input.normalized;
        Vector3 velocity = direction * speed;
        Vector3 moveAmount = velocity * Time.deltaTime;
        
        if (Input.GetKeyDown(KeyCode.Space))
        {
            b = (GameObject)Instantiate(blocks[Random.Range(0, 7)], new Vector3(0, 6, 0), Quaternion.Euler(Vector3.right));
            //b.AddComponent<Rigidbody>().isKinematic = false;
        }
        b.transform.Translate(moveAmount); // <-- Line 27 throws the exception.
    }
}

enter image description here

Source Link
mavish
  • 151
  • 5

Prefab instantiation gives NullReferenceException

I keep getting this error NullReferenceException: Object reference not set to an instance of an object moveBlock.Update () (at Assets/Scripts/moveBlock.cs:27) whenever i press Play in Game or when i press Space while in play. I looked at other questions posted but it is not clear to me whast the solution is, i understand i am missing a GetComponent line but not sure.

using System.Collections;

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

public class moveBlock : MonoBehaviour {

public float speed = 10;
public GameObject[] blocks;
private GameObject b;

void Update()
{
    Vector3 input = new Vector3(Input.GetAxisRaw("Horizontal"), 0, Input.GetAxisRaw("Vertical"));
    Vector3 direction = input.normalized;
    Vector3 velocity = direction * speed;
    Vector3 moveAmount = velocity * Time.deltaTime;
    
    if (Input.GetKeyDown(KeyCode.Space))
    {
        b = (GameObject)Instantiate(blocks[Random.Range(0, 7)], new Vector3(0, 6, 0), Quaternion.Euler(Vector3.right));
        //b.AddComponent<Rigidbody>().isKinematic = false;
    }
    b.transform.Translate(moveAmount);
}

}

enter image description here