Hi,
I was trying to do create a function to check if a GameObject has a certain component and I found this weird bug in Unity. If someone could explain me what is going on, I’d be very happy.
**Here’s how to reproduce the problem:
1.** Create a GameObject
2. Add script to GameObject
private void Awake()
{
Debug.Log(HasComponent<TextMesh>(gameObject));
}
private bool HasComponent<T>(GameObject go)
{
T component = go.GetComponent<T>();
return (component != null);
}
3. Try to start the game WITH AND WITHOUT a TextMesh component attached to the Game Object. You’ll notice that both of the times the console will log ‘True’.
This is really weird, is even returns true if you don’t have a TextMesh component attached. This piece of code works with other components (for example Image), but I’m really wondering why it isn’t working with the TextMesh component.
Thanks in advance again & kind regards,
Nathan