0

I'm new to unity and have a question regarding a segment of code I'm having trouble with. When I declare the object in the main script it properly renders the Blank sprite but when I try calling the PlaceMultiplier function it throws the NullReferenceException. Im hoping to get the fix and also learn from the explanation, thanks!

using UnityEngine; using System.Collections;

public class Die : 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;
    }

}

}

  • Can you share the code where you actually call the `PlaceMultiplier()` method. – Noel Widmer Jun 01 '17 at 10:33
  • @Noel Widmer Yes the call is Die1.PlaceMultiplier (2) where Die1 is declared as Die Die1 an object of type Die – ChingPow Jun 01 '17 at 19:13
  • @NoelWidmer I don't know if Im able to contact you through stack so I'll send you a DM through Twitter. – ChingPow Jun 01 '17 at 19:18
  • No, StackOverflow is a platform about people helping people. It's not about me helping you. We will answer your question on here and a future reader might benefit from the solution as well. – Noel Widmer Jun 01 '17 at 19:20
  • @NoelWidmer thats fine I wasn't sure if you'd be able to see my reply. – ChingPow Jun 01 '17 at 19:24

0 Answers0