2D Toolkit Forum

2D Toolkit => Support => Topic started by: Artifact on June 29, 2013, 03:05:40 pm

Title: private tk2dSpriteCollection gives NullRef exception, no exception if public
Post by: Artifact on June 29, 2013, 03:05:40 pm
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;
}
Title: Re: private tk2dSpriteCollection gives NullRef exception, no exception if public
Post by: unikronsoftware on June 29, 2013, 06:16:23 pm
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.
Title: Re: private tk2dSpriteCollection gives NullRef exception, no exception if public
Post by: Artifact on July 01, 2013, 07:25:55 am
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.
Title: Re: private tk2dSpriteCollection gives NullRef exception, no exception if public
Post by: unikronsoftware on July 01, 2013, 10:30:06 am
That makes 2 of us then :)
Maybe worth firing off a bug report to Unity?