Declarations / Variables

Hi,

A variable is not an object. It’s a variable. In our case, pool is “empty” (= null). We have to explicitely assign an object reference.

With new GameObject[PoolSize], we create a new object of type GameObject[]. Then we assign the object reference to pool.

Because C# does not allow us to do that. This would be possible: GameObject[] pool = new GameObject[5];. However, we cannot use any variables at instance level because we cannot guarantee that the variable and its value exist when we try to access it. For this reason, variables can be used within methods only. When we execute method code blocks, the instance variables do exist. Why? Because that’s how C# works.

Is this what you wanted to know? :slight_smile:


See also:

2 Likes