Where are you setting what “tf” is a reference to, and are you sure you are setting that before this function is called? The most likely issue is “tf” is a reference to nothing at the point this function is being called.
But are you verifying it is being set correctly in the Start function? Or are you just trying to set it with some kind of Find method, and not checking if the result of that Find was null?
But that wasn’t my question. However you are setting tf it appears to be getting set as null, even if you are doing so with a GetComponent. You should check if it is null after you attempt to set it to figure out what is going wrong. You aren’t posting the code for that, or shown setting tf happens before trying to use it, so there isn’t much else that can be helped with.
GetComponent is not require for transform, but that’s another story.
However, depending on how your scene is setup, there is no guarantee of the execution order of Start. So it your Start on the other script is running first, then your variable tf may still be null.
You can try moving your tf variable assignment to Awake, or you could just access the transform directly with…transform.
Such as transform.ChildCount . Both these options may at least help you determine if it’s just one script executing before the other.
There are also ways in Unity to set up script execution order, but I would just test the other options first as they should be easy test.
You instantiate that GO with the CardPositioning component in another Start() method, here:
The instantiation of that object does only cause Awake to be called on the CardPositioning component which you’re trying to access in the last line of that snippet, however Start of that component will run later. Use Awake instead to set the cached transform variable or just remove the caching variable completely and use the transform property instead.
You were right, the Start of the calling script was executed before the other script and tf was null, so accessing it directly didn’t made a difference, but moving it to Awake did.
One more note now that that is resolved. Your cardPrefab is being used to modified the sharedMaterial; is it the instantiated copy that you want to update, rather?