0

I have an assets file under the name of play-button with texture type=sprite2D and Sprite Mode = Multiple, it contains 2 different frames: play-button0 and play-button1

i have attached a c# script file for this asset with the following code:

using UnityEngine;
using System.Collections;

public class Play_btn : MonoBehaviour {

    SpriteRenderer spriteRenderer;
    Sprite s1;
    Sprite s2;
    // Use this for initialization
    void Start () {
        spriteRenderer = GetComponent<SpriteRenderer>();
        if (spriteRenderer.sprite == null)
                        spriteRenderer.sprite = s1;
    }
    void OnMouseDown() {
        Application.LoadLevel("Levels");
    }
    void OnMouseEnter() {
        spriteRenderer.sprite = s2;
    }
    void OnMouseExit() {
        //spriteRenderer.sprite = sprite1;
    }
    // Update is called once per frame
    void Update () {

    }
}

As you can see i am trying to change the frame onMouseEnter and OnMouseExit but when the mouseEnter the play button a null reference error is occurring and a blank object is displaying on the screen, can anyone help me in this problem?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
Sora
  • 2,465
  • 18
  • 73
  • 146

1 Answers1

2

From what I see, the Sprites s1 & s2 are not public (so you could not have assigned it via the inspector), and there's no code where you assign the s1 & s2 variables.

Solution
Make the Sprites s1 and s2 public, and assign them.

Venkat at Axiom Studios
  • 2,456
  • 1
  • 15
  • 25