C
C#17mo 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
ACiDCA717mo 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
eduardoA17mo ago
thanks
Want results from more Discord servers?
Add your server
More Posts
✅ Help with non-nullable field must contain a non-null value when exiting constructor.Following an online tutorial and trying to resolve nullable reference warnings (the tutorial was befC# WPF Login Oracle DatabaseSo I'm making a login system with C# WPF and I use Oracle DB. As I understood oracle it only has one❔ Using mvvm toolkit- Why my view has no access to my command?I got the following Code in the view model: ``` [RelayCommand] public void CalculateAnd❔ Razor Page - Select Tag Helper (form-select): Need help with onchange event for drop-down listIn my APR.cshtml page I have the following drop down list present: `<select asp-for="@Model.ServerNa❔ EF Core setting property with value converter to nulli have a value object ```cs public record ShippingOrderNumber(int Number);``` that i'm mapping to ❔ Some bug i guess**Code that works** ``` Task.Run(() => Static_class_name.START(param1, param2, param3)); ``` *inside❔ Visual Studio not producing templates?Hey everyone, I'm attempting to teach myself C# via this website " https://www.w3schools.com/cs/cs_g❔ Confusing ClientWebSocket.ReceiveAsync() behaviorPicture the following class: ```cs private ClientWebSocket _ws = new(); private Memory<byte> _buffe❔ Better way to do this random password generation programThis program generates a random password then removes duplicates and excludes some chars. I need sug✅ Validation Error with Discord.Addons.HostingI'm trying to use Discord.Addons.Hosting, but it throws an OptionsValidationException . My main look