C
C#2y 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}");
}
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
2 Replies
ACiDCA7
ACiDCA72y ago
because string interpolation uses $ aswell as the placeholder in the snippet you would have to escape it i guess never done it but you could try \
eduardoA
eduardoAOP2y ago
thanks
Want results from more Discord servers?
Add your server