#GetComponent null, why?

1 messages · Page 1 of 1 (latest)

honest ingot
#

Testing with cs var test = _playerPanel.transform.Find( "Vertical Group" ); var test2 = test.Find("Name"); var test3 = test2.GetComponent<TextMeshPro>( ); and test and test2 is good but test3 returns null. What am I missing? Name does have the TextMeshPro :|

celest stag
#

TextMeshProUGUI i think or whatever the name is for it

rocky marten
#

Isn’t it textmeshprogui or something else long like that

past current
#

or TMP_Text

#

this whole thing is bad though. Don't use find.

honest ingot
#

Ah yeah you're probably right. Just noticed TextMeshPro seems to be for 3d objects while UI might have its own

past current
#

if you have the ref to playerPanel, you can directly ref the other ones too.

past current
#

how have you referenced _playerPanel ?

honest ingot
#

I store a ref at start with _playerPanel = GameObject.Find( "PlayerPanel" );

past current
#

Fuck

#

bin it off.

honest ingot
#

Thought storing a ref was more efficient 😳

past current
#

[SerializeField] private GameObject playerPanel;
[SerializeField] private TextMeshProUGUI name;

honest ingot
#

🤔 I don't understand anything

#

I'm 1 month into unity so might need a link explaining what's going on

rocky marten
#

The gif is old but the process is the same

#

Putting what Don posted earlier, you can create a slot in the inspector that you can drag into

#

Then you don’t have to use .Find

honest ingot
#

Oh right, thought that was only for Public variables

past current
#

serialize field exposes it to the inspector, but is private to this class

honest ingot
#

Oh that's awesome. thanks!

past current
#

9/10 things you write as public... shouldnt be public

honest ingot
#

I agree

#

Totally forgot about this, need to refactor a bunch of code now lol

past current
#

basically. if you have any type of Find in your code, you're doing it wrong

rocky marten
#

I don’t believe there’s a performance cost but idk

honest ingot
#

Yeah me neither since I store refs and don't call find every frame

#

but cleaner to just have them init at compiletime i guess

past current
#

your code is now broken

#

its unscalable

honest ingot
#

very true

#

I will have to use Find when working with prefab properties tho?

past current
#

No

honest ingot
#

or maybe not if the script is from the prefab I suppose

past current
#

Also no

rocky marten
#

The only time I ever use find/FindObjectOfType is when using DDOL since it breaks it from the hierarchy meaning I can’t drag reference it

#

I wonder if there’s a better way but idk

honest ingot
#

hmm, not showing up in editor?

past current
#

Then you have errors

honest ingot
#

haha very true