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 ...
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:
...
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:
<...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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 ...
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, ...
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 ...
1
vote
How to call StartCourotine in abstract class?
I'm unable to reproduce this problem. Testing with the following AbstractClass:
...
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 ...
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 ...
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 ...
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 ...
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 ...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
monobehaviour × 32unity × 31
c# × 16
coroutines × 3
architecture × 2
unityscript × 2
gameobject × 2
editors × 2
serialization × 2
inheritance × 2
c++ × 1
collision-detection × 1
3d × 1
game-design × 1
game-loop × 1
optimization × 1
entity-system × 1
sdl2 × 1
component-based × 1
events × 1
collider × 1
.net × 1
oop × 1
entity-component-system × 1
interface × 1