Answer the question
In order to leave comments, you need to log in
NullReferenceException: Object reference not set to an instance of an object. What to do?
1 script.
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using TMPro;
public class XPMan : MonoBehaviour
{
public TextMeshProUGUI currentXPtext, targetXPtext, levelText;
public int currentXP, targetXP, level;
public static XPMan instance;
private void Awake()
{
if (instance == null)
instance = null;
else
Destroy(gameObject);
}
private void Start()
{
currentXPtext.text = currentXP.ToString();
targetXPtext.text = targetXP.ToString();
levelText.text = level.ToString();
}
public void AddXP(int xp)
{
currentXP += xp;
while (currentXP >= targetXP)
{
currentXP = targetXP - currentXP;
level++;
levelText.text = level.ToString();
}
currentXPtext.text = currentXP.ToString();
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class PL : MonoBehaviour
{
public void ButtonPressed()
{
Debug.Log("XPMan");
Debug.Log("instanse");
Debug.Log("AddXP");
; XPMan.instance.AddXP(10);
}
}
Answer the question
In order to leave comments, you need to log in
Well so you explicitly specified that instance = null.
private void Awake() { if (instance == null) instance = null; else Destroy(gameObject); }
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question