Whole Tomato Software Forums
Whole Tomato Software Forums
Main Site | Profile | Register | Active Topics | Members | Search | FAQ
User name:
Password:
Save Password
Forgot your password?

 All Forums
 Visual Assist
 Technical Support
 Can't find references to variable in C# in Unity
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Jorge24
Senior Member

USA
28 Posts

Posted - Aug 11 2020 :  7:00:38 PM  Show Profile  Reply with Quote
I'm programming in C# in Unity (the game engine). When trying to find the places where the bool "AllowGalaxyMenyPopUp" is referenced I get an error that says "Find References is not available because the symbol unrecognized"

I think this happens only when the trying to find reference to it after using "GetComponent<>()"





accord
Whole Tomato Software

United Kingdom
3287 Posts

Posted - Aug 13 2020 :  08:05:01 AM  Show Profile  Reply with Quote
Did you select part of the symbol name? The background color of "Galaxy" is different. When you use the Find feature of Visual Studio, or you just press F3 to continue searching, Visual Studio selects the text you are searching for. In this case, Visual Assist throws this same error message that you show on the screenshot if VA Find References is triggered. If you dismiss the selection first (e.g. via pressing ESC), then it should work after.

Edited by - accord on Aug 13 2020 08:07:24 AM
Go to Top of Page

Jorge24
Senior Member

USA
28 Posts

Posted - Aug 13 2020 :  1:18:03 PM  Show Profile  Reply with Quote
the reason galaxy looks different in the screenshot is just that I had also made a Ctrl+F search for "Galaxy", however, if I go to the bool's declaration at the top of the class, and then do VA's find references, it doesn't find the instances where the bool is accessed through Unity's
GetComponent<>()
.

Here are Visual Assist's "Find References"(Alt + Shift + F) results:





And here is Visual Studio's "Find All References" results (Shift + F12):




As you can see, Visual Studio found 4 more references than Visual Assist.
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19137 Posts

Posted - Aug 14 2020 :  1:22:08 PM  Show Profile  Reply with Quote
I am seeing the same problem here, I am looking into what is going on with these generic functions in C#.

zen is the art of being at one with the two'ness
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19137 Posts

Posted - Aug 15 2020 :  10:14:55 AM  Show Profile  Reply with Quote
VA is not working out the correct type when the generic function is returning a generic type, I have put in a bug report for this:

case=142800

interestingly in the very simple example shown here:

https://docs.unity3d.com/ScriptReference/Component.GetComponent.html

in the unity documentation, the type of the return from GetComponent is set clearly with a new variable before being used, which would obviously help VA to follow what is going on, but is also not how your code is written.

zen is the art of being at one with the two'ness
Go to Top of Page

Jorge24
Senior Member

USA
28 Posts

Posted - Aug 15 2020 :  2:32:05 PM  Show Profile  Reply with Quote
I see, thank you for looking into it and making a bug report. We dont always create a variable to store the value of GetComponent<>() and then use that reference to call a function or get a property of that component. This is less performant because you then have to retrieve a reference to an object from heap and also it takes up more lines of code for not much benefit.

Maybe that was not exactly the reason in this case (I didnt write the code that I screenshotted to be honest) but there are valid reasons to do it that way like I described above. In this case what happened probably was that the first person who worked on this class maybe created a variable that references the component but other people who worked on it later didn't realize a reference already existed so they just did GetComponent again. This kind of things tend to happen when multiple people work on the same file over long periods of time and sometimes they are just trying to get a hotfix done to get rid of a bug or something and there are no strict code reviews. Happens in indie projects or small game studios like the one I'm working at.

The reason why you would create reference to the component first and then access methods/properties through that reference is usually for readability, esp if its used in multiple places. But if you're just trying to use it in a single place in your cs file, then you would just use GetComponent<SomeClass>().MyBool

Edited by - Jorge24 on Aug 15 2020 2:41:25 PM
Go to Top of Page

feline
Whole Tomato Software

United Kingdom
19137 Posts

Posted - Aug 17 2020 :  05:38:42 AM  Show Profile  Reply with Quote
Thank you for explaining. When I started trying to understand the problem I wasn't sure if this was a Unity specific problem, or a feature of the C# language its self that VA did not properly handle. It turns out to be a C# language feature, nothing to do with Unity its self, but it took a bit of reading to figure that out. Always something new to learn

Certainly valid and reasonable C# code, even if not always as clear as it could be, and VA should handle this correctly, hence the bug report. Thank you for the bug report.

zen is the art of being at one with the two'ness
Go to Top of Page

Jorge24
Senior Member

USA
28 Posts

Posted - Aug 17 2020 :  11:22:08 AM  Show Profile  Reply with Quote
I see, thats interesting. Yeah, learning never stops lol
Thanks again for opening a case!
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
© 2023 Whole Tomato Software, LLC Go To Top Of Page
Snitz Forums 2000