C#C
C#3y ago
eduardoA

Making a shortcut for checking null component in unity

Evetime I create a new component in unity, I something like


if (!TryGetComponent(out _rb2D)) {
            Debug.LogError($"There is no rigidbody component in the {transform.name}");
        }


So I created a shortcut

<CodeSnippets xmlns="http://schemas.microsoft.com/VisualStudio/2005/CodeSnippet">
    <CodeSnippet Format="1.0.0">
        <Header>
            <Title>Unity null check components</Title>
            <Shortcut>ucnc</Shortcut>
            <Description>This code snippet, will allow you to check you component, to see if they are null</Description>
            <Author>Eduardo Gomez</Author>
        </Header>
        <Snippet>
            <Code Language="csharp">
                <![CDATA[
                    if(!TryGetComponent(out $ComponentName$))
                    {
                        Debug.LogError($"There is no $ComponentName$ component in your {transform.name}");
                    }
                ]]>
            </Code>
          <Declarations>
            <Literal>
              <ID>ComponentName</ID>
              <ToolTip>The component name that you want to check for null</ToolTip>
            </Literal>
          </Declarations>
        </Snippet>
    </CodeSnippet>
</CodeSnippets>


but when I use it, I get this

    if (!TryGetComponent(out )) {
            Debug.LogError(ComponentName


this is not what I want, where is my
 Debug.LogError($"There is no $ComponentName$ component in your {transform.name}");
message? and why there is nothing here
  if (!TryGetComponent(out )) { 
for me to edit? and hi it dosent close the if
Was this page helpful?