Now that we have a reference to the Voxel Object component, we can set its animation frame. Just like when you reference a different entity, you will need to check that the voxel object is not null before you can do anything to it.
- In the onCollisionEnter function, in a new line after you created your voxelObject variable, write an if-statement to check that the voxel object is not null.
- Inside the if-statement, call the showVoxelFrameAtKey(1) on voxelObject to set its animation to the second frame (the first frame is 0).
Note: Where is ‘!=null’?
You may have noticed that we just wrote if(voxelObject)
instead of if(voxelObject != null)
. These actually work the same way, because null
is what is called a falsy value. It isn’t the same thing as false, but if you use it in an if-statement, for example, it will get treated like false.
There are some other interesting falsy values, like NaN
(Not a Number) or even -0
. You can see a full list here.