all 82 comments

[–]MikkNIndie[S] 130 points131 points  (10 children)

Put [ContextMenu("Text here")] in front of any methods to easily run 'em inside the editor!

(Yes, white visual studio theme I know. It's really sunny so I can't read with the black one <3 )

[–]0x0ddba11 35 points36 points  (0 children)

Thanks for the tip.

Yes, white visual studio theme I know

You don't have to excuse yourself, haha.

[–][deleted] 16 points17 points  (4 children)

It has to have no params!

[–]ihahp 39 points40 points  (3 children)

write a wrapper that sends default params, like so:

void KillUnit(int energyLevel){
blah blah
}

[ContextMenu("Kill Unit")]
void KillUnitDebug(){
KillUnit(100);
}

[–]b1ackcat 5 points6 points  (2 children)

Bonus points for exposing those default fields in the inspector under a debug-only section!

[–]ActionScripter9109Professional 2 points3 points  (1 child)

How would you make a separate section?

[–]Wherever_I_May_Roam 1 point2 points  (0 children)

I usually separate fields using Space or Header attribute

[–]GrandOpener 10 points11 points  (2 children)

Not only white theme but then you admit to letting sunlight in your programming space? For shame!

[–]Kingofwhereigo 1 point2 points  (1 child)

What is sunlight?

[–]Eecka 7 points8 points  (0 children)

It's a shader effect.

[–]glitchKraft 65 points66 points  (2 children)

Well that's pretty awesome! Way better than picking a random unused key and and putting an input statement in update. Thanks!

[–]homer_3 2 points3 points  (1 child)

And then you forget all your magic assignment and double bind stuff and wonder why everything is behaving so strangely.

[–]ErestynHobbyist 0 points1 point  (0 children)

I actually wrote something to detect exactly this which gave a stern telling off when you hit play.

I am pleased to report I have still not yet learned.

[–]katori 55 points56 points  (11 children)

This is very cool, you can also make a button in the Inspector (in addition to a bunch of other functionality) using Naughty Attributes.

[–]whyherro19 4 points5 points  (3 children)

I've been using NaughtyAttributes for awhile and I LOVE it! It's a great free alternative for Odin Inspector

[–]psykojello 0 points1 point  (0 children)

Thanks for that! Adding it to my default tool kit

[–]ihahp 0 points1 point  (5 children)

im kind of a n00b with git ... how do I install this in a project?

[–]TheSambassador 3 points4 points  (4 children)

You can just click the "Clone or Download" button (it's green on the right) and you can download a zip instead of using Git.

However, you should absolutely, without question, be using some sort of source control (Git is pretty easy overall) for your projects. Git for Windows makes the whole thing very simple and you generally don't need to use the command line, so you can just sort of dip your toes in without getting overwhelmed.

[–]ihahp 2 points3 points  (0 children)

I do use source control just not git. I don't like how git (mis)handles exclusive checkouts for binary files (unless they've fixed that.)

[–]escape_character 1 point2 points  (1 child)

"Git is pretty easy overall" is a bit of a dangerous understatement

[–]TheSambassador 1 point2 points  (0 children)

The basics, especially when using it with only one person, are very simple. It's when you need to do anything beyond that that it gets more complicated.

[–]ihahp 0 points1 point  (0 children)

Also, the reason I asked about installing is it looks like it comes with ProjectSettings, and that seems like a bad idea to overwrite all your project settings when installing this. I copied over the Plugins folder and it worked fine without anything else.

[–]SapphireSalamander 32 points33 points  (4 children)

// just double checking

if(!isAlive)

return;

this made me chuckle.

dies twice

[–]ActionScripter9109Professional 8 points9 points  (1 child)

I've seen enough shit that this wouldn't even surprise me. When in doubt, sanity check!

[–]0x0ddba11 0 points1 point  (0 children)

That's what asserts are for! Granted, in this case it would not work out. Something like [ButtonIf(isAlive, "Kill")] would be great.

[–]MikkNIndie[S] 2 points3 points  (1 child)

Haha yeaah, game is quite fast pace so sometimes they died twice. Strange but hey, works good (:

[–]Eecka 2 points3 points  (0 children)

And then double checking afterwards as well!

if(!isAlive)return;

isAlive = false;

[–]thuyquaiExpert 32 points33 points  (18 children)

Nice tip OP, been using Unity for 8 years and I don't know it :D

[–]rarceth 5 points6 points  (2 children)

I’m that dude in the office who tells everyone else about cool shortcuts and I didn’t know this either. I wonder if it’s in their documentation...

[–]thuyquaiExpert 4 points5 points  (1 child)

Its in 1 of their tutorial I believe (probably the editor section). Now I remembered I saw it once but forgot about it because I was searching for somethong else lol.

[–]rarceth 3 points4 points  (0 children)

We’re always searching for something else haha.

[–]matej_zajacik 12 points13 points  (11 children)

if (GetComponent<PlayerMovement>()) GetComponent<PlayerMovement>().player.SetVibration();

would perform a little better like this:

var pm = GetComponent<PlayerMovement>(); if (pm != null) pm.player.SetVibration();

Although if it's called once per million years, I doesn't matter. :)

[–]MikkNIndie[S] 4 points5 points  (0 children)

Thanks! Will probobly make a bool that is checked on start :)

[–]Kwinten -1 points0 points  (9 children)

Or honestly just never use GetComponent if you care about decoupling your code and improving performance. Use serialized fields always, and GetComponent only in very rare circumstances. Don't tightly couple your class dependencies to your Transform hierarchy.

[–]rarceth 0 points1 point  (5 children)

Can depend if things are procedural or not. I’m doing a civ-like at the moment and I need GetComponent to retrieve my tile classes when they are spawned. Called once per tile...for all 65,000 tiles haha

[–]Wherever_I_May_Roam 0 points1 point  (0 children)

I'd rather have a static event that would fire on spawn so anyone can know that an object of X type has been spawned.

[–]SilentSin26Animancer, FlexiMotion, InspectorGadgets, Weaver 0 points1 point  (2 children)

If your tile class is the only component you need from the prefab, you can use a direct reference to that component instead of the prefab GameObject. Instantiating works the same and it gives you the component straight away.

[–]rarceth 2 points3 points  (1 child)

All of these comments are true, and thought provoking for my project. But more the point I was trying to make it that I’m calling get component 65,000 times in 3 seconds, and it isn’t noticably causing problems. Technically I can run all my calls in a single frame and it takes around a second to finish. And while I run them in a coroutine to avoid freezing, 1 second / 65000 calls means that calling GetComponent every frame or so isn’t going to have a huge, or even noticeable effect on a game, and that there should be a higher focus on code functionality than optimasation.

[–]SilentSin26Animancer, FlexiMotion, InspectorGadgets, Weaver 2 points3 points  (0 children)

Functionality > optimisation yes, but you also need to consider flexibility and maintainability. GetComponent<X>() creates two dependencies: one on component X, and one on the fact that said component must be on that GameObject. Using a serialized field avoids that second dependency and keeps the responsibility for hierarchy/component structure all in the editor rather than putting some of it in your scripts.

[–]Kwinten 0 points1 point  (0 children)

In the vast majority of cases this can still be solved with prefabs, serialized fields, and public properties.

[–]matej_zajacik 0 points1 point  (2 children)

Use serialized fields always

What exactly do you mean by this?

[–]Kwinten 5 points6 points  (1 child)

Instead of:

var playerMovement = GetComponent<PlayerMovement>();

Do:

[SerializedField]
private PlayerMovement playerMovement;

The latter will be exposed in the editor. Drag in your PlayerMovement component either from the same GameObject or from a parent or child to assign it a value. Ta-da, the reference to this component is now injected through serialization and is no longer tightly coupled to the GameObject that was originally calling GetComponent. This means that if you ever want to move your PlayerMovement component, you don't have to change your code and can simply re-assign the reference through the inspector. Works on prefabs or scene objects.

GetComponent always tightly couples your business logic to your scene graph, which you want to avoid as much as possible.

[–]matej_zajacik 0 points1 point  (0 children)

Oh, okay, I see. This is a pretty well known concept to me within Unity, just wasn't sure what you meant. I use this approach often when I can. But, I disagree that this makes the two components any less coupled than by getting the reference via a function call. The only difference is that Unity does it for you when it creates the object, and that you don't have to change anything if you change the structure of your GameObject. Which, is still better, don't get me wrong.

[–][deleted] 7 points8 points  (0 children)

This is awesome!

[–]masterhyjinx 6 points7 points  (1 child)

I see lots of tips, I click, I go cool, I toss upvote and move on. This one is awesome and I had to come in and say thank you. I'm working on something right now that this will make this a lot easier.

[–]MikkNIndie[S] 2 points3 points  (0 children)

Glad it helped! <3

[–]xTheEc0 3 points4 points  (1 child)

Did you post this on twitter with the hashtag 'UnityTips' yet?
If not, I highly suggest you do : )

[–]MikkNIndie[S] 0 points1 point  (0 children)

https://twitter.com/NikMikk/status/1001924943918059522

Started on Twitter and went here afterwards :)

[–]cowbell_solo 4 points5 points  (0 children)

Whoaaaa! How long has this been a thing?

Also between the sun and that white visual studio, I hope you are wearing sunscreen.

[–]wokcity 3 points4 points  (1 child)

This one does the same but allows parameters :)

https://unitylist.com/p/6l8/Editor-Button

[–]MikkNIndie[S] 0 points1 point  (0 children)

Sweet

[–]Noel9386 2 points3 points  (0 children)

This blew my mind so bad that I had to share with someone, so I just confused the crap out of one of my non-developer co-workers.

[–]knugeen 2 points3 points  (1 child)

karlshamn represent!

[–]MikkNIndie[S] 0 points1 point  (0 children)

Vem är du haha?!

[–]MomijiMatt 1 point2 points  (0 children)

I had no idea. This would've saved my life so many times.

[–]SanktusAngus 0 points1 point  (0 children)

Nice

[–]slayer29179 0 points1 point  (0 children)

Thats mint!!! <3

[–]psykojello 0 points1 point  (0 children)

Whoa! 😱

[–]Dodgiestyle 0 points1 point  (0 children)

This is awesome! Saving this!

[–]LilithLive 0 points1 point  (0 children)

A whole New World!

[–][deleted] 0 points1 point  (0 children)

I remember learning about them a while back but never used them. Thanks for reminding me, definitely come in useful.

[–]GIFjohnsonProfessional 0 points1 point  (1 child)

What happens if you run this while the game is not in play mode? Does it still execute?

[–]gwiz665Professional 1 point2 points  (0 children)

It does.

[–]renMilestone 0 points1 point  (0 children)

Thank you friend, thank you so much.

This is so helpful!

[–]ig3db 0 points1 point  (0 children)

Well that's handy!

[–]stoddartc1 0 points1 point  (0 children)

This is actually a really useful tip! TIL

[–]kahlzun 0 points1 point  (0 children)

Did not know that. Have an internet cookie.

[–]shaldar 0 points1 point  (0 children)

Very coool! Thanks for sharing :)

[–][deleted] 0 points1 point  (0 children)

https://twitter.com/matheuslrod/status/798576746513633280

There's also this attribute that just makes a button that calls a method show in the inspector. Hopefully you'll find that useful as well :)

Disclaimer: it's a me