c# - Error when play is clicked:NullReferenceException: Object reference not set to an instance of an object -
i pretty new c# , unity,so question basic. has bugged me long, couldn't on after research on internet.
anyway, here question, have got error
nullreferenceexception: object reference not set instance of 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 button view 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 initialization void start () { //here error logged activebutton = gameobject.find("streakbtn").getcomponent<achievementbutton>(); createachievement("streak","testtitle","this description",3,0); activebutton.click(); } // update 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 error logged if(sprite.sprite == neutral) { sprite.sprite = highlight; achievementlist.setactive(true); } else { sprite.sprite = neutral; achievementlist.setactive(false); } } }
your awake()
method starts lowercase, while should start uppercase: void awake()
adjust awake
method this:
void awake() { sprite = getcomponent<image>(); debug.log("awake has been called, sprite " + sprite); debug.log("neutral " + neutral); debug.log("highlight " + highlight); }
to see whether method gets called @ , whether 3 variables (sprite
, neutral
, highlight
) defined (they must not null
).
you can see output of debug.log()
in console
, can found in unity when click on menu item view -> console
. use debug.log()
see what's going on in scripts.
if neutral
, highlight
variables null
in awake
, need assign them manually via inspector, i.e. drag , drop sprites them achievementbutton
game object in editor.
Comments
Post a Comment