I code that i cant test

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.

using UnityEngine;

public class advancetestAI : MonoBehaviour

{

private Transform target = GameObject.Find(“player”).transform.position;

private GameObject hp_target = GameObject.Find(“player”);

player_hp deal = hp_target.GetComponent<player_hp>();

private bool in_dect = distance < (dectrng + 1f);

private bool in_atk = distance < (atkrng + 1f);

private bool roam = distance > dectrng;

private float distance;

private float roam_spd;

private float roam_var;

private float rot_count;

private float move_count;

private float count_max = 50f;

private float dectrng = 10f;

private float atkrng = 3f;

private float dely = 3f;

private float timer;

private float lvl = 1f;

private float srt_hp = 100f * lvl;

private float _current_hp;

void Start(){

roam_spd = Random.Range(1, 10);

roam_var = Random.Range(-100, 100);

current_hp = srt_hp;

}

void Update(){

distance = Vector3.Distance(target, transform.position);

isdead();

if(roam){

wonder();

}

if(in_dect){

transform.Rotation += transform.LookAt(target) * 5 * Time.deltaTime;

transform.position += transform.forward * 5 * Time.deltaTime;

}

if(in_atk){

transform.Rotation += transform.LookAt(target) * 5 * Time.deltaTime;

if(Time.deltaTime > timer){

attack();

wait();

}

}

}

private void wait(){

timer = Time.deltaTime + dely;

}

private void wonder(){

if(rot_count < count_max){

transform.Rotate(Vector3.right * roam_var * Time.deltaTime);

rot_add();

}

else if(move_count < count_max){

transform.position +=  transform.forward * roam_spd * Time.deltaTime;

mov_add();

}

if(move_count = count_max && rot_count = count_max){

roam_spd = Random.Range(1,10);

roam_var = Random.Range(-100,100);

count_reset();

}

}

private void rot_add(){

rot_count += 1;

}

private void mov_add(){

move_count += 1;

}

private void count_reset(){

move_count = 0;

rot_count = 0;

}

private void attack(){

deal.player_current_hp -= Random.Range(10,20);

}

private void isdead(){

if(current_hp < 1){

gameObject.destroy;

}

}

}

Is this for real ?

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 ? :smile: (which you would have gotten from not writting the unecessary “private” at each line).

It wont compile btw.

So many problems I don’t even know where to start.

Sorry its just when i code i like to use variables that are abbreviated

@LaneFox Ok if its that bad then please could you start with a list of whats wrong

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?

1 Like

Writing complicated code is wrong. Complex code is ok. Manageably complex is better. Simple code is the best.

Why? It just makes your code less readable and harder to understand, for other people (and probably for you too).

4 Likes

You do realise that’s the job of a compiler right? You should get something like this.

private Transform target = GameObject.Find(“player”).transform.position;

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.)

player_hp deal = hp_target.GetComponent<player_hp>();

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._

player_hp deal = hp_target.GetComponent<player_hp>();

Error. (Unexpected symbol)

… 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.

1 Like
void Update()
{
    distance = Vector3.Distance(target, transform.position);
    isdead();
    if (roam)
    {
        wonder();
    }
}

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.Rotation += transform.LookAt(target) * 5 * Time.deltaTime;

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”.

in_dect is my abbreviation for in detection

Ok im starting to remake this code would this be good for the creation of the varibels

using UnityEngine;

public class advancetestAI : MonoBehaviour

{

private GameObject player;

private Transform target;

private bool in_detection_range;

private bool in_attack_rang;

private bool no_target;

private float detection_range;

private float attack_range;

private float distance_target;

private float roam_speed;

private float roam_rotation_speed;

private float rotate_counter;

private float move_counter;

private float counter_max;


void Start(){


player = gameObject.Find(“Player”);

target = gameObject.Find(“Player”).transform.position;

distance_target = Vector3.Distance(target , transform.position);

detection_range = 10;

attack_range = 3;

in_detection_range = distance_target < detection_range;

in_attack_range = distance_target < attack_range;

no_target = distance_target > detection_range;

move_counter = 0;

rotate_counter = 0;

counter_max = 50;

roam_rotation_speed = Random.Range(-360 , 360);

roam_speed = Random.Range(-10 , 10);

}

}

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?

distance_target = Vector3.Distance(target , transform.position);
in_detection_range = distance_target < detection_range;
in_attack_range = distance_target < attack_range;
no_target = distance_target > detection_range;

I suspect that these variables should be recalculated every frame rather than getting their values only once at the initialization.

target = gameObject.Find(“Player”).transform.position;

target field is of type Transform.
position property returns a Vector3 value. You can’t assign a Vector3 value to a Transform variable.

I guess it’s written in MS Word or something like that. It likes to convert “normal” quotation marks to the “correct” ones.

Best reply: Learn

8 Likes

@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.

Ok this now what the remake is like

using UnityEngine;
public class advancetestAI : MonoBehaviour
{
private GameObject player;
private Vector3 target;
private float detection_range;
private float attack_range;
private float distance_target;
private float roam_speed;
private float roam_rotation_speed;
private float rotate_counter;
private float move_counter;
private float counter_max;

void Start(){
player = gameObject.Find(“Player”);
detection_range = 10;
attack_range = 3;
move_counter = 0;
rotate_counter = 0;
counter_max = 50;
roam_rotation_speed = Random.Range(-360 , 360);
roam_speed = Random.Range(-10 , 10);
}
//~~~~~~~~~~~~~~~~~~~~~^Declare and set variables^
void Update(){
target = player.transform.position;
distance_target = Vector3.Distance(target , transform.position);
//~~~~~~~~~~~~~~~~~~~~~~none in range so wonder
if(distance_target > detection_range){
if(rotate_counter < counter_max){
transform.Rotate((Vector3.right * roam_rotation_speed * Time.deltaTime), Space.Self);
rotate_counter += 1;
}
if(move_counter < counter_max && rotate_counter == counter_max){
transform.position += transform.forward * roam_speed * Time.deltaTime;
move_counter += 1;
}
else if(move_counter == max_counter && rotate_counter == counter_max){
roam_rotation_speed = Random.Range(-360, 360);
roam_speed = Random.Range(-10, 10);
move_counter = 0;
rotation_counter = 0;
}
}
//~~~~~~~~~~~~~~~~~~~~~~player in range, go to
if(distance_target < detection_range && distance_target > attack_range){
transform.LookAt(target);
transform.position += transform.forward * 10 * Time.deltaTime;
}
//~~~~~~~~~~~~~~~~~~~~~player in attack range
if(distance_target < attack_range){
transform.LookAt(target);
}
}
}

I have confidence that this code is a lot better than the original

lol…

1 Like

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

1 Like

Pascal Case is the term you’re looking for.

What’s wrong with adding to transform.position?

1 Like