Version: Unity 6.7 Alpha (6000.7)
LanguageEnglish
  • C#

VisualElement.GetOrAddComponent

Suggest a change

Success!

Thank you for helping us improve the quality of Unity Documentation. Although we cannot accept all submissions, we do read each suggested change from our users and will make updates where applicable.

Close

Submission failed

For some reason your suggested change could not be submitted. Please <a>try again</a> in a few minutes. And thank you for taking the time to help us improve the quality of Unity Documentation.

Close

Cancel

Declaration

public ref T GetOrAddComponent();

Returns

T A reference to the live component data. Like VisualElement.GetComponent, the reference is valid until a component is added to or removed from this element.

Description

Returns a reference to the component of type T attached to this element. If the component is not attached yet, it is added first.

When the component is already attached, this method behaves exactly like VisualElement.GetComponent and the component data is not changed. When it is not attached, a new component is created and added the same way as VisualElement.AddComponent. The new component is created with the type's UxmlCreateInstanceMethodAttribute method when the type has one, otherwise with default values.