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.
}
}

Quaternion.Euler(Vector3.right)means "Rotate this object exactly 1 degree around the x axis," which is very likely not what you meant. \$\endgroup\$