单击播放时出错:NullReferenceException:对象引用未设置为对象的实例
本文关键字:设置 对象 实例 对象引用 播放 出错 NullReferenceException 单击 | 更新日期: 2023-09-27 18:06:06
我对C#和unity很陌生,所以我的问题可能是基本的。然而,它困扰了我这么长时间,即使我在互联网上研究了它,我也无法克服。
不管怎样,这是我的问题,我有这个错误
NullReferenceException:对象引用未设置为object AchievementButton。click(((位于资产/脚本/实现系统/实现按钮。cs:25(成就经理。开始(((在资产/脚本/实现系统/实现管理器。cs:30(
当点击播放或点击按钮查看我的成就列表时。
AchievementManager脚本:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class AchievemenetManager : MonoBehaviour
{
public GameObject achievementPrefab;
public Sprite[] sprites;
public AchievementButton activeButton;
public ScrollRect scrollRect;
// Use this for initialization
void Start ()
{
//here is where the error is logged from
activeButton = GameObject.Find("Streakbtn").GetComponent<AchievementButton>();
CreateAchievement("Streak","testTitle","this is a description",3,0);
activeButton.click();
}
// Update is called once per frame
void Update () {
}
public void CreateAchievement(string category,string title,string description,int points, int spriteIndex)
{
GameObject achievement = (GameObject)Instantiate(achievementPrefab);
SetAchievementInfo(category, achievement,title,description,points,spriteIndex);
}
public void SetAchievementInfo(string category, GameObject achievement, string title, string description, int points, int spriteIndex)
{
achievement.transform.SetParent(GameObject.Find(category).transform);
achievement.transform.localScale = new Vector3(1, 1, 1);
achievement.transform.localPosition = new Vector3(0, 0, 0);
achievement.transform.GetChild(0).GetComponent<Text>().text = title;
achievement.transform.GetChild(1).GetComponent<Text>().text = description;
achievement.transform.GetChild(2).GetComponent<Text>().text = points.ToString();
achievement.transform.GetChild(3).GetComponent<Image>().sprite = sprites[spriteIndex];
}
public void ChangeCategery(GameObject button)
{
AchievementButton achievementButton = button.GetComponent<AchievementButton>();
scrollRect.content = achievementButton.achievementList.GetComponent<RectTransform>();
achievementButton.click();
activeButton.click();
activeButton = achievementButton;
}
}
AchievementButton脚本:
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
public class AchievementButton : MonoBehaviour
{
public GameObject achievementList;
public Sprite neutral, highlight;
private Image sprite;
void awake()
{
sprite = GetComponent<Image>();
}
public void click()
{
//here is where the error is logged from
if(sprite.sprite == neutral)
{
sprite.sprite = highlight;
achievementList.SetActive(true);
}
else
{
sprite.sprite = neutral;
achievementList.SetActive(false);
}
}
}
您的awake()
方法以小写开头,而它应该以大写开头:void Awake()
像这样调整Awake
方法:
void Awake()
{
sprite = GetComponent<Image>();
Debug.Log("Awake has been called, sprite is " + sprite);
Debug.Log("neutral is " + neutral);
Debug.Log("highlight is " + highlight);
}
查看该方法是否被调用,以及是否定义了所有三个变量(sprite
、neutral
、highlight
((它们不能是null
(。
您可以在Console
中看到Debug.Log()
的输出,当您单击菜单项View -> Console
时,可以在Unity中找到该输出。经常使用Debug.Log()
来查看脚本中发生了什么。
如果neutral
和highlight
变量是Awake
中的null
,则需要通过Inspector手动分配它们,即在编辑器中将一些精灵拖放到AchievementButton
游戏对象中。
NullReferenceException
发生在您尝试使用尚未分配给的变量时。在这种情况下,我认为它是在为click()
方法下的sprite.sprite
。我会检查以确保为sprite提供值的awake()
方法始终在click()
之前运行neutral
或highlight
提供值之前,将neutral
或highlight
分配给sprite.sprite
。
neutral = ?? ;// something of type Sprite
highlight = ?? ;// something of type Sprite