Field is never assigned to, and will always have its default value 'null' with public initialized variables
I have this code:
public class followPlayer : MonoBehaviour
{
#pragma warning disable 0649
[SerializeField] private GameObject player = null;
}
and I get this error:
Field 'followPlayer.player' is never assigned to, and will always have its default value 'null'
The user is supposed to set the field in the inspector. Of course it's not supposed to get set using code.
And, no, making a field public isn't the only way to make a field visible in the inspector. Making a field public means other classes are supposed to access it, the inspector just apparently shows it. If it's supposed to show in the editor but other classes aren't supposed to access it, like in this case, you should make it private add [SerializeField]
.
I already looked for other posts in Unity Answers, which didn't work because everything said there didn't work for some reason. Making it public didn't work and as said above, is not recommended in this case. Instantiating it with null
didn't work. Instantiating it with default
, didn't work. Adding #pragma warning disable 0649
also didn't work. Closing Visual Studio and opening it again didn't work.
But it's still there. How do I get rid of this warning?