© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
4 replies
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}");
        }
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>
<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
    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}");
 Debug.LogError($"There is no $ComponentName$ component in your {transform.name}");
message? and why there is nothing here
  if (!TryGetComponent(out )) { 
  if (!TryGetComponent(out )) { 
for me to edit? and hi it dosent close the if
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

NullReferenceException but checking for Null?
C#CC# / help
2y ago
❔ Unity property unityaction invoke
C#CC# / help
3y ago
❔ making something wait in unity script
C#CC# / help
3y ago
casting required for nullable even after checking for null
C#CC# / help
12mo ago