1

I'm programming a JRPG off of a tutorial online to try and learn Unity and C# code. This is a program for creating a menu of enemy units to attack:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
//using UnityEngine.Experimental.UIElements;

public class CreateEnemyMenuItem : MonoBehaviour {

    [SerializeField]
    private GameObject targetEnemyUnitPrefab;

    [SerializeField]
    private Sprite menuItemSprite;

    [SerializeField]
    private Vector2 initialPosition, itemDimensions;

    [SerializeField]
    private KillEnemy killEnemyScript;

    private void Awake()
    {
        GameObject enemyUnitsMenu = GameObject.Find("EnemyUnitsMenu");

        GameObject[] existingItems = GameObject.FindGameObjectsWithTag("TargetEnemyUnit");

        Vector2 nextPosition = new Vector2(this.initialPosition.x + (existingItems.Length * this.itemDimensions.x), this.initialPosition.y);

        GameObject targetEnemyUnit = Instantiate(this.targetEnemyUnitPrefab, enemyUnitsMenu.transform) as GameObject;

        targetEnemyUnit.name = "Target" + this.gameObject.name;
        targetEnemyUnit.transform.localPosition = nextPosition;
        targetEnemyUnit.transform.localScale = new Vector2(0.7f, 0.7f);
        targetEnemyUnit.GetComponent<Button>().onClick.AddListener (() => selectEnemyTarget());
        targetEnemyUnit.GetComponent<Image>().sprite = this.menuItemSprite;

        killEnemyScript.menuItem = targetEnemyUnit;
    }

    public void selectEnemyTarget()
    {

    }
}

At line 29 (where targetEnemyUnit is instantiated) during game tests I get a NullReferenceException error which I figure means Instantiate isn't working (it also highlights here but doesn't in Visual Studio). I'm working largely off of a tutorial but this matches and I'm not sure what exactly I'm doing wrong here.

1201ProgramAlarm
  • 30,320
  • 7
  • 40
  • 49
  • [https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it](What is a null reference exception) – Derek C. May 19 '20 at 04:05
  • 1
    Does this answer your question? [What is a NullReferenceException, and how do I fix it?](https://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Jawad May 19 '20 at 04:16
  • Could you please add a link to the tutorial so we can see where you are getting your information and code from? – Jee May 19 '20 at 04:57

2 Answers2

1

Either targetEnemyUnitPrefab was not set in the inspector, in which case you just need to click on the GameObject that contains this script, in the inspector drag and drop your prefab into 'targetEnemyUnitPrefab' or enemyUnitsMenu was not tagged properly, in which case click on the 'GameObject' that is supposed to be tagged and make sure in the top right of the inspector it is tagged as 'EnemyUnitsMenu'

note when you create a new tag and have a game object selected it doesn't apply the tag, you have to go back to the game object and select it from the bottom of the drop down.

enter image description here

GameObject targetEnemyUnit = Instantiate(this.targetEnemyUnitPrefab, enemyUnitsMenu.transform) as GameObject;
vasmos
  • 2,168
  • 1
  • 7
  • 20
0

okay you have a private object as a prefab -> private GameObject targetEnemyUnitPrefab <- and you didnt put anything in that prefab so its nothing. so you are getting nullreference exception because of it. make a public game object prefab and drag your prefab on it. Or make something like this before instatiate the object ~ targetEnemyUnitPrefab= GameObject.Find("targetEnemyUnitPrefab");~ or whatever tag it has. have a nice day.