the variable:
public CharacterDatabase characterDB;
is never initialized, so accessing its properties and methods will throw an error.
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Good Day, I'm programming a game with Unity, but I cannot resolve this error:
NullReferenceException: Object reference not set to an instance of an object
CharacterManager.UpdateCharacter (System.Int32 selectedOption) (at Assets/CharacterManager.cs:47)
CharacterManager.Start () (at Assets/CharacterManager.cs:17) could you help me?
Thanks in advance of the answer
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class CharacterManager : MonoBehaviour
{
public CharacterDatabase characterDB;
public Text nameText;
public SpriteRenderer artworkSprite;
int selectedOption = 0;
//start is called before the first frame update
void Start()
{
UpdateCharacter(selectedOption);
}
public void NextOption()
{
selectedOption++;
if(selectedOption >= characterDB.CharacterCount)
{
selectedOption = 0;
}
UpdateCharacter(selectedOption);
}
public void BackOption()
{
selectedOption--;
if(0 > selectedOption)
{
selectedOption = characterDB.CharacterCount - 1;
}
UpdateCharacter(selectedOption);
}
private void UpdateCharacter(int selectedOption)
{
Character character = characterDB.GetCharacter(selectedOption);
artworkSprite.sprite = character.characterSprite;
nameText.text = character.characterName;
}
}
the variable:
public CharacterDatabase characterDB;
is never initialized, so accessing its properties and methods will throw an error.