-1

I am pretty new to C# and unity,so my question could be basic. However it has bugged me for so long, that I couldn't get it over even after I research it over the internet.

Anyway, here is my question, I have got this error

NullReferenceException: Object reference not set to an instance of an object AchievementButton.click()(at Assets/scripts/AchievementSystem/AchievementButton.cs:25) AchievementManager.Start()(at Assets/scripts/AchievementSystem/AchievementManager.cs:30)

when clicked on play or when click a button to view my list of achievements.

AchievementManager Script:

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);
        }
    }
}
Nain
  • 1,204
  • 1
  • 12
  • 17
  • 1
    [What is a `NullReferenceException` and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Soner Gönül Aug 03 '15 at 07:58

2 Answers2

2

Your awake() method starts with lowercase, while it should start with uppercase: void Awake()

Adjust your Awake method like this:

void Awake()
{
    sprite = GetComponent<Image>();
    Debug.Log("Awake has been called, sprite is " + sprite);
    Debug.Log("neutral is " + neutral);
    Debug.Log("highlight is " + highlight);
}

to see whether the method gets called at all and whether all three variables (sprite, neutral, highlight) are defined (they must not be null).

You can see the output of Debug.Log() in Console, which can be found in Unity when you click on menu item View -> Console. Use Debug.Log() frequently to see what's going on in your scripts.

If the neutral and highlight variables are null in the Awake, you need to assign them manually via Inspector, i.e. drag and drop some sprites to them to AchievementButton game object in the Editor.

Arx
  • 384
  • 2
  • 13
0

NullReferenceException occurs when you try to use a variable you haven't yet assigned to. In this case i think it's sprite.sprite under your click() method. I would check to make sure your awake() method that gives sprite a value always runs before click() You assign neutral or highlight to sprite.sprite before you give a value to either neutral or highlight.

neutral = ?? ;// something of type Sprite
highlight = ?? ;// something of type Sprite
weirdev
  • 1,388
  • 8
  • 20
  • @surajmaharaj okay thanks I'll remove that from my answer. Your problem is most likely related to sprite not being defined before your click() method. – weirdev Aug 03 '15 at 08:23
  • @surajmaharaj If awake isn't called before click, sprite wont be defined. However, your use of neutral and highlight certainly are a problem. – weirdev Aug 03 '15 at 08:48
  • You need to assign some value to `neutral` and `highlight` before you use them. I'm not sure what you want to use them for so I can't tell you what to define them as. You could do this in awake() or in click(), whichever is appropriate for your design. – weirdev Aug 03 '15 at 08:56
  • btw - checkout @Arx 's answer to make sure your awake method is actually being called. – weirdev Aug 03 '15 at 09:09