I get this error pretty often… sometimes, after I grab an object or variable from another script, Unity tells me that I have an unexpected symbol “;” at the end of whatever line I grab the variable/object. I’m using C#. What is the problem here?
An example of the code that makes it do that:
attackTarget = GameObject.Find("Main Camera").GetComponent<"Clicker">.attacked;
attackTarget and attacked are both transforms.
attackTarget = GameObject.Find(“Main Camera”).GetComponent.attacked;
Remove the quotes around Clicker. In C# Generics, you don’t put strings in there, just the Type/Class itself.
EDIT: woops, and you need parenthesis: GetComponent is a method:
attackTarget = GameObject.Find(“Main Camera”).GetComponent().attacked;
Looks like there’s at least a couple of errors in that line of code. First of all, the generic version of GetComponent() that you’re using expects a type in the angle brackets, not a string literal. Also, since GetComponent() is a function, you need a pair of parentheses to indicate that the function should be invoked.
With those problems corrected, the code should look like this:
attackTarget = GameObject.Find("Main Camera").GetComponent<Clicker>().attacked;
Edit: Also, note that if your ‘Main Camera’ object is tagged as ‘MainCamera’, you can access it (indirectly) via Camera.main, saving the cost of an object look-up.
For me, that error usually means that a function isn’t enclosed in the proper amount of brackets {}. Usually the offending code is buried in a nested function. The error may not even be in the line that the ; is in. ( I use javascript, but same idea. ) Make sure when you copy code from another script that you reformat the code to fit within the function you are curmudgeoning it into.
If you post more of the script so we can see the whole thing, we may help spot the error better than just one line.
In this particular case at least, the errors were in fact in the line of code that was posted.
Well, the GetComponent<> format is a generic function; therefore, it requires a type. Depending on what the your Clicker is a class or a component, you can access it as the following:
As a class/type (script):
attachTarget = GameObject.Find("Main Camera").GetComponent<Clicker>().target;
As a component:
attachTarget = GameObject.Find("Main Camera").GetComponent("Clicker").transform;
Thanks… I keep grabbing bits of code from Unity’s examples, but those are all in javascript.
Many (if not all) of code samples in the Unity docs have a little language selector at the top-right that lets you swap them with JS/C#/Boo.