- Home /
Why does the code stops?
First of all excuse me for my bare English. If i run the code with the commands (//) enabled it will not do the calculation (will not print "is voorbij") (i tested this by printing a text message after the calculation is done). With those lines of code as a command // it works fine. Can somebody explain this to me?
public int count;
public GameObject enemy;
public Transform spawn;
void Start () {
count = (int)Time.realtimeSinceStartup;
EnemySpawnController ESC = new EnemySpawnController();
ESC.CheckIfFree();
}
public void GoTOLocation(string name) {
print ("Searching for gameobject with name:" + name);
spawn.Find ("1");
print ("x." + spawn.transform.position.x + "/" + spawn.transform.position.y);
Vector2 diff;
//diff.x = spawn.transform.position.x-gameObject.transform.position.x;
//diff.y = spawn.transform.position.y-gameObject.transform.position.y;
print ("is voorbij");
if (x != enemy.transform.position.x) { gameObject.transform.Translate(Vector2.right * diff.x); }
if (y != enemy.transform.position.y) { gameObject.transform.Translate(Vector2.up * diff.y); }
}
Answer by TheSOULDev · Aug 10, 2017 at 06:17 PM
// is commenting, meaning that anything after // in a line won't be compiled.
Answer by arain55 · Aug 10, 2017 at 06:22 PM
Hi @Robgar2001 , those are comments and they are not executed by the compiler and are only there for the people writing the code or reading the code to inform them what that line of code does. eg:
//this line prints Hello world to the unity console
print("Hello world")
the line above will print Hello world to the unity console and the comment above describes what it does.
//print("Hello world")
that will print nothing as it is ignored by the compiler, in other words it is not executed.
there are two types of comments multiline and one line
look here for more info: http://www.c-sharpcorner.com/uploadfile/puranindia/comments-in-C-Sharp/
and please before you start using unity learn a programming language, such as C# and that will avoid questions such as this which are unnecessary
Answer by Robgar2001 · Aug 10, 2017 at 07:52 PM
@arain55 @TheSOULDev I know i know but i mean when i remove the // the code won't print the "is voorbij" message. So those two lines avoid the computer to print the "is voorbij" message. And i want to know why those lines avoid that. (I know how the command system works)
Oh right , sorry. Um it's probably because theres an error, are you getting an error? if so what is it and change line 16
Vector2 diff;
to :
Vector2 diff = new Vector 2 ((spawn.transform.position.x-gameObject.transform.position.x),(spawn.transform.position.y-gameObject.transform.position.y))
and remove the two lines underneath it and see if it solves anything
I changed it still nothing, also no error. I do see that the cords of the spawn transform are not correct. I changed it to an GameObject but still nothing. $$anonymous$$aybe theire is something worng with the loading of the spawn GameObject, that could explain why he doesn't want to do the calculation at the vector2.
Your answer
Follow this Question
Related Questions
Collision issues with Vector2.Reflect 1 Answer
How to find total distance from start to end of an array which stores positions in vector3? 2 Answers
Rigidbody2D.Velocity = Vector2 not changing X Position 1 Answer
Not able to fire projectiles at mouse position 2 Answers
moving objects with transform position 2 Answers