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