我对C#和团结都很陌生,所以我的问题可能是基本的。然而它长期以来一直困扰着我,即使我在互联网上研究它也无法解决它。
无论如何,这是我的问题,我有这个错误
NullReferenceException:未将对象引用设置为的实例 对象AchievementButton.click()(at 资产/脚本/ AchievementSystem / AchievementButton.cs:25) AchievementManager.Start()(在 资产/脚本/ AchievementSystem / AchievementManager.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 Script:
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);
}
}
}
答案 0 :(得分:2)
您的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
)。
您可以在Debug.Log()
中看到Console
的输出,当您点击菜单项View -> Console
时,可以在Unity中找到该输出。经常使用Debug.Log()
来查看脚本中发生了什么。
如果neutral
中的highlight
和null
变量为Awake
,您需要通过Inspector手动分配它们,即将一些精灵拖放到{编辑器中的{1}}游戏对象。
答案 1 :(得分:0)
NullReferenceException
。 在这种情况下,我认为您之前将sprite.sprite
方法下的click()
。我会检查以确保您的awake()
方法为精灵提供值始终在click()
之前运行neutral
或highlight
分配给sprite.sprite
您为neutral
或highlight
提供了值。
neutral = ?? ;// something of type Sprite
highlight = ?? ;// something of type Sprite