Where to start? I’d recommend you read up a bit on how variables and functions work firstly, as a solid understanding here is the best way to troubleshoot these problems as they crop up. There are also conventions people use which make it tons easier to read - such as variables always starting with a lower case letter, and functions or classes starting with an upper case one. Unity uses this convention in its own functions, classes and variables, so you’d be shooting yourself in the foot if you don’t use it too.
Still, the really important thing is that the name of a variable or function is the same every time, and I mean exactly the same - a computer is reading it after all, not a human. Captalisation is important too, so I’d recommend you go through your script and make sure everything’s written as it should be.
Here’s a few spelling and capitalisation errors at a glance;
MonoBaviour - The namespace should be MonoBehaviour.
attack - You declare the function as Attack() but later forgot the capitalisation when calling it from Start().
getKeyUp - Should be GetKeyUp.
Damage - You later use the variable damage, so make sure that’s what you declare it as too.
Also your script (if you’re going with convention) is itself a class, so it should be called Player, not player, both in the script and for its file name. Sounds anal, but it’s a convention for a good reason.
Next up, make sure that the { } curley braces are always closed afterwards. One is left open for the Player class (ie. the whole script) and another for the Attack() function. Nothing will compile properly, and your game won’t run if you’ve left those out. They can be easy to miss sometimes though, but being consistent in how you lay out your script visually can help you spot them.
The next error is that nobody knows what an opponent is. You correctly declared a type for each of your other variables, but;
public static opponent;
…doesn’t really mean anything. The compiler will look at it and think - OK it’s public, so it’s accessible outside of this class, it’s static, so it lasts for the entire length of the program, and next it’s looking for a type (like an int, float, bool etc.) and it sees “opponent”. But what’s “opponent”?
As a human, I guess from your script that “opponent” is, being lower case, the name of the variable (this comes after the type though), and I’m guessing its type should be either a Transform*, a GameObject, or a custom class that’s declared elsewhere. If you use Transform then your later use of it would make sense without the addition of “.transform” to the dot notation (because it’d already be looking at a transform).
Back to being anal about parentheses, If statements should use curly braces for the second bit. For example,
if (1 > 0)
{
// Do stuff in here
}
Not these ones → ( )
With your if statement you also need to think about how you’re nesting things. Look at the difference between the following lines, the first of which is yours;
if (opponent != null && Vector3.Distance(opponent.position, transform.position < range)
{
// Stuff that happens
}
// ---------- //
if (opponent != null && Vector3.Distance(opponent.position, transform.position ) < range)
{
// Stuff that happens
}
Count how many brackets for each direction are in yours compared to mine, and look at where “< range” needs to go.
Finally, at least for the important stuff, GetComponent() needs to know what to get somehow. The brackets after it, and after every other function, are for overloads. You can stick extra stuff in them depending on what the function needs to operate. In this case you can input a type, and it’ll throw back a component of that type, or you can use a name to decide which component you want. If you don’t choose something then a normal game object might have a ton of components attached. Examples of both ways to use the GetComponent() function are in the unity documents here; Unity - Scripting API: GameObject.GetComponent I’m guessing that you’re trying to find a specific script on the opponent / enemy’s game object
Other considerations;
- There’s probably no need to check for an attack in the Start() function.
- I can’t tell from your script why you made opponent static, but I don’t know what you have planned later or how your game is designed to work.
- It might be easier to keep tabs on everything later in development if you have checking for player inputs in one function, and then have what happens in a second one.
- “name”, that you used as a variable, is already used in Unity for a variable “Object.name”, it’s best to avoid that and try something more specific like “characterName”, or “playerName”.
- Why use GetKeyUp()? Does your player character attack when you release the Q key, or would you prefer to use GetKeyDown() so they attack when it’s pressed down. Most games use the latter, so unless you’re charging a special attack or some other mechanic, most people would want to register when the key goes down in this case.
Just try to chew through it bit by bit and eventually it’ll seem second nature. Tutorials on general scripting are a great place to start - have a look through the basic Unity ones, as there’s some good material out there now for beginners. Just understanding the terminology and layout of Classes, Functions and Variables is really important! Hope that helps, at least a little bit.
- Note the capital “T”, because it’s the Transform class, not an instance of it.