Hello Guest

Author Topic: private tk2dSpriteCollection gives NullRef exception, no exception if public  (Read 5333 times)

Artifact

  • Newbie
  • *
  • Posts: 9
    • View Profile
Why will the following code give me a null ref exception:

Code: [Select]
    private tk2dSpriteCollection sCollection;

// Use this for initialization
void Start ()
{
        sCollection.spriteCollection = GetComponent<tk2dSprite>().Collection;   
}

And the following code doesn't give me an exception, I'm not assigning anything to the public object, the only thing I'm doing is changing the access modifiers from private to public...

Code: [Select]
    public tk2dSpriteCollection sCollection;

// Use this for initialization
void Start ()
{
        sCollection.spriteCollection = GetComponent<tk2dSprite>().Collection;
}

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
That doesn't make any sense at all. sCollection is unassigned in both, and .spriteCollection will throw a null ref exception if it were public or private. Print out the contents to see what sCollection is.

Artifact

  • Newbie
  • *
  • Posts: 9
    • View Profile
I know it doesn't make any sense. :)

If I write it out with Debug, it write null when it's private, and when it's public it writes the name of the collection on the GameObject..... I have no clue why it does that.

unikronsoftware

  • Administrator
  • Hero Member
  • *****
  • Posts: 9709
    • View Profile
That makes 2 of us then :)
Maybe worth firing off a bug report to Unity?