我是统一的新手,并且对我遇到问题的一段代码有疑问。当我在主脚本中声明对象时,它会正确呈现空白精灵,但是当我尝试调用 PlaceMultiplier 函数时,它会抛出 NullReferenceException。我希望得到修复并从解释中学习,谢谢!
使用 UnityEngine;使用 System.Collections;
公共类死:MonoBehaviour {
public Sprite Dice1;
public Sprite Blank;
public Sprite Multiplier1;
public Sprite Multiplier2;
public Sprite Multiplier3;
public Sprite Multiplier4;
public Sprite Multiplier5;
public Sprite Multiplier6;
public Sprite Multiplier7;
public Sprite Multiplier8;
// Use this for initialization
void Start () {
if ((this != null)) {
this.gameObject.GetComponent<SpriteRenderer> ().sprite = Blank;
}
}
// Update is called once per frame
void Update () {
}
public void PlaceMultiplier(int multiple){
if (multiple == 2) {
this.gameObject.GetComponent<SpriteRenderer> ().sprite = Multiplier1;
}
if (multiple == 3) {
this.gameObject.GetComponent<SpriteRenderer> ().sprite = Multiplier2;
}
if (multiple == 7) {
this.gameObject.GetComponent<SpriteRenderer> ().sprite = Multiplier3;
}
if (multiple == 8) {
this.gameObject.GetComponent<SpriteRenderer> ().sprite = Multiplier4;
}
if (multiple == 9) {
this.gameObject.GetComponent<SpriteRenderer> ().sprite = Multiplier5;
}
if (multiple == 10) {
this.gameObject.GetComponent<SpriteRenderer> ().sprite = Multiplier6;
}
if (multiple == 15) {
this.gameObject.GetComponent<SpriteRenderer> ().sprite = Multiplier7;
}
if (multiple == 25) {
this.gameObject.GetComponent<SpriteRenderer> ().sprite = Multiplier8;
}
}
}