Here is all your code. I have added comments so you can see what exactly is happening:
//OnTriggerEnter2D, which runs the command in the block if the object hits another 2D
//object
void OnTriggerEnter2D (Collider2D other)
{
//If the object that this gameObject hits has the tag player
if (other.gameObject.tag == "Player")
{
//Run the Coroutine DoSomething()
StartCoroutine (DoSomething());
}
}
//Coroutine DoSomething
IEnumerator DoSomething()
{
//As long as true is true (this effectively means this code will run infinitely)
while (true)
{
//Do Other Commands
DoMoreStuff();
}
//Normally this would stop the couroutine, but the Engine never gets here
//since its still doing DoMoreStuff() forever;
yield return null;
}
What this code essentially does is activate a Couroutine when the gameObject hits an object with the tag Player. This couroutine will then rune the while(true), which since has the arguement true will run forever until something sets it to false. This would also have the negative side-effect of crashing, since it would repeat infinately, never go to the next section and will crash the engine eventually.
It never reaches the yield return null, which should stop the couroutine, since its still busy doing the while(true) forever. If you were to set it to while(false), then it would skip that whole while section and end the Couroutine for that frame. It will then come back in the next frame, but since there is nothing else do to (since while(false)), then it will simply end it and repeat later on.
There is no real reason to make a Couroutine with the above script, it would have been better to up into an Update/FixedUpdate/LateUpdate or inside the OnTriggerEnter2D. The reason you use a Couroutine is to make a script that keeps repeating itself, but you can choose how long it waits before it repeats, such as after 2 frams or 4 seconds.
It would be much more useful if you put the yield return null inside the while loop to stop the whole couroutine until some time:
IEnumerator DoSomething()
{
while (true)
{
DoMoreStuff();
yield return new WaitForSeconds(5f);
}
}
When you launch this code, it will activate the while(true) untill it reaches the yield section. It will then stop the Coroutine (effectivly stopping the while() method aswell) and wait 5 seconds. It will then repeat where it left of, which is in the end of the while loop. Then it will simply go back to the start of the while loop, keep doing DoMoreStuff(); then stop and wait for 5 seconds to do the same thing all over again.