Skip to main content
8 votes
Accepted

Unity Hybrid ECS with "old way"

It is perfectly possible to mix both styles. The same GameObject can have some of its functionality implemented with the new ECS system and other functionality in classic MonoBehaviour events. What ...
Philipp's user avatar
  • 122k
6 votes
Accepted

How can I give a MonoBehaviour field a default value that depends on the object?

Here's an alternative solution that will copy the current name when the component is added in the editor or at runtime: ...
DMGregory's user avatar
  • 136k
4 votes

How can I give a MonoBehaviour field a default value that depends on the object?

So I had a bit of a "rubber duck debugging" moment with this question. Obviously the easiest way to do it is not to default the field at all, but to add a runtime wrapper for the field like this: <...
Dan Hulme's user avatar
  • 187
4 votes

How to make Lists (and Arrays) in Unity only take GameObjects of a certain "type"

If you only want to store GameObject types that contain a specific Component element, the easiest solution is to use the ...
Gnemlock's user avatar
  • 5,293
3 votes
Accepted

How to reference nameof private serialized field in unity custom inspector

Use your interface Handily enough, you already have your answer. Leave the speed field public, but never pass around or declare references to that class. Only ever ...
Ed Marty's user avatar
  • 5,249
3 votes
Accepted

Does MonoBehaviour have a method that is called in editor when the object is created?

Yes, OnValidate() gets called only in the Editor (including play mode in-editor) in a few situations: When the component is added to a game object When a prefab ...
DMGregory's user avatar
  • 136k
2 votes
Accepted

Pattern for scripts that should only update when in trigger range

You don't need the trigger and update code to be written in separate scripts. Collision messages like OnTriggerEnter/Exit will be called even on disabled ...
DMGregory's user avatar
  • 136k
2 votes
Accepted

Gameobject does not derive from MonoBehaviour

I found my mistake: The script was NOT in the "Editor" folder at first. Then, at some point, I moved it into the "Editor" folder. I didn't notice which effect this had. Now when I ...
tmighty's user avatar
  • 919
2 votes

Public value null after assigning it from a different script

The problem you’re running into is likely due to an incorrect script execution order. The Start method on all scripts with the same priority which are loaded at the ...
Ed Marty's user avatar
  • 5,249
1 vote

Waiting for object's Awake() after loading a new scene synchronously

From Unity’s docs: When using SceneManager.LoadScene, the loading does not happen immediately, it completes in the next frame. This semi-asynchronous behavior [...] can be confusing because load ...
Ed Marty's user avatar
  • 5,249
1 vote
Accepted

How to connect the Data Layer (Models) with the Visual/Interactive Layer (MonoBehaviours)?

The idiomatic way of using Unity is that your should not separate your game architecture into models, views and controllers. A gameObject should contain its own data, logic and presentation, ...
Philipp's user avatar
  • 122k
1 vote

Two Colliders on the same GameObject for different purposes

As DMGregory pointed out, my solution was on the right track. What I do differently now, is that now the Health behaviour detroys its parent. I had an exception when I tried this the first time, but ...
G_hi3's user avatar
  • 111
1 vote

How to call StartCourotine in abstract class?

I'm unable to reproduce this problem. Testing with the following AbstractClass: ...
DMGregory's user avatar
  • 136k
1 vote
Accepted

Unity, WaitUntil in a self-made message box class

Unity doesn’t really support modal dialogs in the way you want, but there are some workaround, like setting the time scale to zero while it’s displayed, and adding a click-blocking later between the ...
Ed Marty's user avatar
  • 5,249
1 vote

How do I convert an interface into a MonoBehavior in Unity?

Try (_ti as MonoBehaviour) MonoBehaviour is spelled the British English way - with ou. But do not get the impression that Unity does consistently use British ...
Philipp's user avatar
  • 122k
1 vote
Accepted

Is it efficient to have a only-data MonoBehaviours on lots of game objects?

Unless you are saving massive amount of information on those scripts, there is nothing performance-wise that you should worry about. If the only thing they do is have some getters/setters or something ...
Tom Tsagkatos's user avatar
1 vote

Public value null after assigning it from a different script

Others have answered this already, but it's caused due to execution order - the start of your adder is called before the assign. Personally, I'd recommend doing the opposite - instead of setting it ...
Andrew Baumher's user avatar
1 vote

Unity3D MonoBehaviour in .dll loses serialized data when dll is updated

Solved. The issue was that I had renamed the class and in doing so, it provides a missing script error and dumps the data. Since my editor automatically adds the required script I did not notice this ...
GorillaOne's user avatar

Only top scored, non community-wiki answers of a minimum length are eligible