Debug.Log explanation...

I’ve read the script reference on this, but I’m still confused as to how to use this to find errors in a script. Do I place this line in the script I want to log? What does it log? Sorry if this is a really silly beginner’s question, but this seems like a very helpful feature I’m not fully understanding or using while I code.

Thanks

Debug.Log shows you variables that you want to check.
For exmaple:

1.I need to check distance of two objects:

    dist = Vector3.Distance(transform.position,bullets[0].transform.position);   
    Debug.Log(dist);

2.Check if something works.

void OnCollisionEnter(Collision collisioner)
{
   Debug.Log("Collided with something");
}

Debug.Log is just used to output some information. You would put it in places in your code where you either think you might have trouble in the future, or you’re currently getting unexpected results, and you want to see the values of variables, the results of calculations, or the state of objects.

for example, you might have some sort of function that returns a Vector3 location, and want to see exactly what the value is returned:

	Vector3 test = Vector3.zero;
		test.x = Random.Range(-1000, 1000);
		test.y = Random.Range(-1000, 1000);
		test.z = Random.Range(-1000, 1000);

		Debug.Log(test);

You can also use it tell you whether objects are null, put in your own remarks, and generally format your debugging info any way you see fit:

Renderer render = gameObject.GetComponent<Renderer>();
		Debug.Log("renderer on object " + gameObject.name + " is " + render);

In this case, if there is no renderer on the game object, the log output will show “renderer on object gameObject is [null]”

so you would know that something is going wrong in somewhere.

Debug.Log is just a function. It is essentialy same as “print” in php, WriteLine in C# etc. The only difference is it writes directly into unity console.

It logs whatever you give it.

Debug.Log("Write this") 

Will display: “Write this” in console. Of course you can insert any object as parameter and toString will automatically be called on it and then written out in console.