I have a code that i have not tested yes and cant for a while so i owuld like to know that would this script work “in theory”, and i would like to know any errors or things i could fix. Also this is the most complicated code i have made.
Hey I’m curious about what happened to the missing letters in variable names, did you spill coffee on your keyboard or maybe you “coded” this from a mobile phone and needed the horizontal space ? (which you would have gotten from not writting the unecessary “private” at each line).
For one thing, a lot of the things you’re doing in the variable assignments can’t be done outside a function - GameObject.Find, for instance.
What do you mean about not being able to test it though? You’re not going to be able to make much progress without seeing if your changes compile/work, and “debugging by forum” is not a recommended method. Do you not have a computer that can run Unity?
Error. (CompareBaseObjectsInternal can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function.)
Error. (CompareBaseObjectsInternal can only be called from the main thread. Constructors and field initializers will be executed from the loading thread when loading a scene. Don’t use this function in the constructor or field initializers, instead move initialization code to the Awake or Start function._
… If the compiler gets this far there are dozens more errors to find. I found three errors in the first six lines (ignoring white space). It gets worse as you go on.
isdead() may call gameObject.Destroy() that destroys the current object. If it does, then wonder() method may throw an exception because you can’t rotate a destroyed object.
transform.LookAt(…) doesn’t return anything, its return type is void. It instantly rotates the object according to the specified target and returns nothing.
if (Time.deltaTime > timer) {
This condition will be true only once, while timer is zero. After that it will always be false, because Time.deltaTime is always less than Time.deltaTime + dely unless the delay is zero or negative. Is it “delay” btw?
As a non-native English speaker writer I can guess what “in_atk” means but have no idea about “in_dect”.
still, the privates are most likely useless, and since we are living in a world after assembly, indentation is not a sinful thing to do. A lot of things you can still move back to class-levelm like detection_range = 10, or attack range = 3. The principle is, is that if it’s a method you’re using (GetComponent, Vector3.Distance.etc), then it should be in a method, and if it’s not (23, bool, 3.14f) then it CAN go right after declaring it.
And one last thing:
Tell me, why do you have an empty line right after each line of code?
@alexzzzz
A transform field contains a object’s position and rotation, a Vector3 field contains 3 floats that are the position of each axis in the word space. It is possible to change a Vector3 to a transform because they have the same information but you can’t change a transform into a vector3 because transform is a vector3 and a rotation of an object
also in_attack_range, in_detection_range, and no_target are bools not floats
The target field can contain either null or a reference to an instance of Transform class or a reference to an instance of a class derived from Transform (e.g. RectTransform). It doesn’t matter how Transform itself is arranged inside, what properties and fields it has (it has no fields).
You can’t assign a Vector3 value directly to a Transform variable, because Vector3 is neither Transform nor derives from it.
Rules are pretty simple. Not intuitive maybe, but simple. You have to get used to it.
I’m going to play a game, where you start from 100 points, and I’ll remove some points based on how bad your mistakes are
Ok, where should I start?
You know what, let’s start from the classname
A class should use camelCase (and the first letter should be uppercase aswell) - 2
“Advance” - 1
Now the class in general
You didn’t use indentation, this is not Assembly - 5 (because of readability)
Variables
You could make the whole thing shorter if you set the value of variables upon declaring them, if they are primitives or a string - 4
GameObject.Find - 1 (if it were in update, or elsewhere in the code, and there would be more, you would get - 3)
“Advance”:
Not even transform.Translate, instead you’re adding to transform.position - 3
Logic:
line 49-54: you put a LookAt into both if’s, you could use a nested if, and save space that way - 3
You always either used > or <, what happens if the value is thar one single possibility you missed? - 3
line 37 - 46 you can save space with nested ifs again - 3
you could use an else if at line 49, and save space that way too - 3
You still didn’t run/tell us the reason why you can’t run this code in unity - 5
This sums up to: 66
//If you have any suggestion for another points to subtract or think, that I subtracted too much/ not enough for something, just tell me and I’m going to edit this
This is a D according too wikipedia in america and a really bad C in england (well, according to wikipedia, if it’s incorrect, please tell me)
EDIT NO.1:
changed Advance/1 to -3 points instead of -5, and changed grade from D- to D