Using code tags properly

If you see someone who needs to know about code tags, please link them to this post:

Please use code tags when posting code.

You can “tag” your code by typing 984347--130826--Screenshot 2015-03-16 10.33.58.png around your code.

There is no overt “Code” tag button that automatically tags a selection as code.

That being said, however, we can Insert code directly into the post using the “Insert Code” button:

This will bring up an “Insert” window were we can paste our code and choose our language:
984347--103141--Screenshot 2014-06-04 12.39.53.png

This will insert this code with Forum Markup that looks like this:
984347--103142--Screenshot 2014-06-04 12.43.25.png
Note: that when manually inserting code, we can choose the language by using “=CSharp”

This is the final code in the post:

using UnityEngine;
using System.Collections;

public class Explode : MonoBehaviour {

    public GameObject explosion;
    public ParticleSystem[] effects;

    void OnCollisionEnter2D (Collision2D collision) {
        if (collision.gameObject.tag == "Hat") {
            Instantiate (explosion, transform.position, transform.rotation);
            foreach (var effect in effects) {
                effect.transform.parent = null;
                effect.Stop ();
                Destroy (effect.gameObject, 1.0f);
            }
            Destroy (gameObject);
        }
    }
}

Note: the code is linked to the documentation with hyperlinks, so if you need to find out more about any piece of pasted code, follow these links to the documentation.

Hope this helps!

If you don’t use code tags, the code can be difficult to read:

using UnityEngine;
using System.Collections;

public class Explode : MonoBehaviour {

public GameObject explosion;
public ParticleSystem effects;

void OnCollisionEnter2D (Collision2D collision) {
if (collision.gameObject.tag == “Hat”) {
Instantiate (explosion, transform.position, transform.rotation);
foreach (var effect in effects) {
effect.transform.parent = null;
effect.Stop ();
Destroy (effect.gameObject, 1.0f);
}
Destroy (gameObject);
}
}
}

41 Likes

I think maybe a bbcode button for the code tags should be added to the quick reply it would make things alot easier… but also the code tags can be written in all lower case such as this(Of course without the quotes)
“[“code][/code”]”
so people could also hand write them…

//code tags used with all lowercase letters
5 Likes

Having code tags in the quick reply is on my features request list. I hope the webdev team will have time when Unity Developer Network settles in…

/me wonders if it is worth a point on feedback…

2 Likes

FINALLY!!! people lately have been spammed code blocks all over the threads… good post ^^

Excellent post!

Yeah!!! We need it. i don’t want to switch to Advance to just to add CODE…

You can just type the code tags yourself; you don’t need to switch to anything.

–Eric

2 Likes

hmmm… Thanks for Trick… Eric. I’ll do.

//  OMG

for(i=0;i<age;i++){
     Body.head.speak("Thanks alot");
}

//Thank you :)
5 Likes

I echo that… Code tag shortcut on quick reply please! I don’t want to type bb tags any longer, that’s sooo 2000’s :).

Fixed that for you

//  OMG

for(i=0;i<Mathf.Infinity;i++){
     Body.head.speak("Thanks alot");
}

//Thank you :)

Infinite thanks that is.

OFF:
Also, the “+ Reply to Thread” button should be gone when you click it… I’ve clicked that many times thinking it was the “Post Reply” button and BAM! reply is gone! BAM, BAM, BOO!!

1 Like

It would be really nice if the line-spacing issue was fixed when copying and pasting source code from code boxes (copied from above):

//  OMG

 

for(i=0;i<Mathf.Infinity;i++){

     Body.head.speak("Thanks alot");

}

 

//Thank you :)

It inserts extra new lines.

These extra new lines do not occur when copying and pasting code from the printable version of threads:
http://forum.unity3d.com/printthread.php?t=143875&pp=20&page=1

Also, I think you guys should replace the default logo in the printable version with the Unity logo :wink:

The new forum theme is really nice but where did the CODE tag button go ? It’s most of the time already hard enough to read people’s code here but without it easily accessible it’s a pain in you know where.

Would be nice if everyone could have the ability to edit a post but only to add CODE tags when missing :smile:

This is a question I was just asking myself today.

They can be manually added, at this point, but I’ll try to find new info and post it here.

_met44:

(afaict) There is no overt “Code” tag button, which automatically tags a selection as code.

We can still manually insert out code tags:

BUT!

That being said, we can Insert code directly into the post using “Insert/Code”:

This will bring up an “Insert” window were we can paste our code and choose our language:
984347--103141--Screenshot 2014-06-04 12.39.53.png

This will insert this code with Forum Markup that looks like this:
984347--103142--Screenshot 2014-06-04 12.43.25.png
Note: that when manually inserting code, we can choose the language by using “=CSharp”

This is the final code in the post:

using UnityEngine;
using System.Collections;

public class Explode : MonoBehaviour {

    public GameObject explosion;
    public ParticleSystem[] effects;

    void OnCollisionEnter2D (Collision2D collision) {
        if (collision.gameObject.tag == "Hat") {
            Instantiate (explosion, transform.position, transform.rotation);
            foreach (var effect in effects) {
                effect.transform.parent = null;
                effect.Stop ();
                Destroy (effect.gameObject, 1.0f);
            }
            Destroy (gameObject);
        }
    }
}

Hope this helps!

1 Like

On a side note, if you copy the code and paste it:

It pastes into Monodevelop without line numbers or added spaces between lines.
It pastes into TextEdit on the Mac with line numbers and coloured text with the hyperlinks to the docs intact!
It pastes into Mac Mail.app with line numbers and coloured text with non-functional hyperlinks…

1 Like

Thank you very much Adam, you must be busy and still took the time to check it, it’s very nice.

I would never have found it… However, I didn’t stop using the tag myself because of the hiden button, I simply wrote it manually. I was more worried junior users of the forum would never find it… (would be best as a red blinking CODE button actually ^^')

I do see your point _met44! I gather Aurore did try hard to get an easy “code tag” feature, but this new software package wasn’t that forgiving. At least we do have “Insert Code”!

1 Like

Only if you use rich text; if you use plain text, then it gets pasted as normal without line numbers. It would be nice if there was a “select all code” button though.

–Eric

Thanks…

Anyway, I’ll just use the insert method.

Testing:

var poopiness = 0;
var desiredPoopiness = 3;

if(poopiness > desiredPoopiness){goLoco();}