-1
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ As an aside, Quaternion.Euler(Vector3.right) means "Rotate this object exactly 1 degree around the x axis," which is very likely not what you meant. \$\endgroup\$
    – DMGregory
    Jul 19 '20 at 16:39
  • 1
    \$\begingroup\$ Does this answer your question? NullReferenceException in Unity \$\endgroup\$
    – Hellium
    Jul 19 '20 at 17:02
  • \$\begingroup\$ i looked at that , not really. I mean it looks too complicated. I think it was solved as per below not sure why it works though. \$\endgroup\$
    – mavish
    Jul 19 '20 at 17:08
2
\$\begingroup\$

In this part:

    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);

In the last line, you try to access b, however, it will only be valid after you press space once. You probably need to check if b is valid before using it.

if (b) {
    b.transform.Translate(moveAmount);
}

Note that if you press space multiple times, you will be missing the previous references to b. And you will only move the last one created.

\$\endgroup\$
2
  • \$\begingroup\$ thanks for that. I think the condition if (blocksSpawned[blocksSpawned.Count-1] != null) blocksSpawned[blocksSpawned.Count-1].transform.Translate(moveAmount); makes it all work but not sure why. I am now also using a list to store the spawned gameobjects. \$\endgroup\$
    – mavish
    Jul 19 '20 at 17:08
  • \$\begingroup\$ If you want to update the created instances at every update call, you can loop through your new vector calling translate for each GameObject. Assuming you keep only valid references, you don't need the if statement. (a foreach statement should work) I can't say much about your new code, but from the previous, the issue was that you were trying to access b before you assigned it. \$\endgroup\$
    – MateusMP
    Jul 20 '20 at 18:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.