Hot answers tagged

11 votes
Accepted

Why is there only one variable displayed in the Inspector?

public GameObject Obj => _obj; This is not a variable, it's a property. It's equivalent to this: ...
user avatar
  • 112k
6 votes
Accepted

How to use C# 8.0 with Godot 3.3.2?

The issue was coming from TargetFramework! If the option .net5.0 is not supported by Godot, there is another option to have full System.Runtime feature of C# v8 <...
user avatar
5 votes
Accepted

Why doesn't Unity use properties from C# scripts instead of fields?

As LifGwaethrakindo noted, properties can have side effects. Imagine we had this property on a character: ...
user avatar
  • 3,395
5 votes
Accepted

Should we have multiple scripts doing only one thing, or one large script?

Scripts take up some space in memory, but a larger script covering the same job would generally take up the same amount or more. So you don't save anything that way. If you have many instances of ...
user avatar
  • 112k
5 votes
Accepted

In Unity, why won't the speed change when I'm crouching?

At the end of your Update method, you have this block: if (isGrounded == false) { Speed = AirSpeed; } else { Speed = 20; } ...
user avatar
  • 111k
5 votes

how do i NOT let this object spawn at a certain area?

A simple solution which covers a wide variety of circumstances which could invalidate spawn locations can be to simply check if the randomly generated position is within a "forbidden zone" ...
user avatar
  • 111k
4 votes

Unity 2D animated tiles library: IsSelected variable not working or not present within RuleTileEditor.cs

Open Unity On top of Unity window, you will see your version number. Will look something like, Unity 2020.3.8f1 Go to: https://github.com/Unity-Technologies/2d-extras On Unity 2d Extras Github, check ...
user avatar
  • 41
4 votes
Accepted

Unity - How to create a game-save with a preview image?

Since you're working with JSON format already, you could simply include the preview image as a Base64 blob that can be decoded on the fly when reading the savegame list. Assuming you have access to a ...
user avatar
  • 3,904
4 votes

Should we have multiple scripts doing only one thing, or one large script?

Yes, having fewer components in the scene (which might or might not correlate with many distinct script files you have) means that Unity has less management overhead, so reducing components by ...
user avatar
  • 111k
4 votes
Accepted

If I can add a Text above my player/object without a canvas then what do I need the canvas for?

The TextMesh component (or its superior alternative TextMeshPro) is perfectly fine if a naked text is all your want to show. That's what it's for. But a world-space canvas has benefits when you have ...
user avatar
  • 111k
4 votes

Why use Animator.StringToHash?

This has to do with the speed of looking up data. When you want to assign a new value to a parameter on an animator, Unity has to find where that parameter sits in memory, to be able to put the new ...
user avatar
  • 112k
3 votes
Accepted

How do I expose a field of an abstract type to the Inspector?

It's possible natively since 2019.3 release via [SerializeReference] attribute. Unity's SerializeReference documentation.
user avatar
  • 46
3 votes

How to restrict an AssetReference to a specific type?

I know this question is old but it came up as the first result. In the current version (1.19), the right approach is to create your own type using generics as you mentioned. ...
user avatar
3 votes
Accepted

Unity Texture2DArray is too noisy

...
user avatar
  • 112k
3 votes
Accepted

How to make a GameObject occasionally change direction in Unity?

...
user avatar
  • 112k
3 votes

Can't figure out math for converting negative indices of my voxels and chunks to positives

I think your problem might be partially due to the way the % operator works with negative numbers. I have a vague memory of tripping up in the same way - I present ...
user avatar
3 votes

I need a game over (UI image) to appear after timer reaches 0

there aren't any specific codes or videos too my knowledge. If you only look for code or videos that do exactly what you want to do, then you are limited to making only what has been made before. You ...
user avatar
  • 112k
3 votes
Accepted

disabling or enabling a public component

Solution Try replacing your Component type to a MonoBehavior. Then script.enabled = false ...
user avatar
  • 713
3 votes

Change color of point lights automatically over time

Your light's color isn't changing because it uses the Light component, not Renderer. Simply add support for the ...
user avatar
3 votes

How can I add a exit to the random maze generator?

My approach would be to find a different maze generation algorithm which guarantees that you get a maze where every point on the grid is part of a single connected graph. Or in other words: One where ...
user avatar
  • 111k
3 votes

Singleton issue

While I can't be certain, you have most likely a loop error in your //logic bloc. But why does it work when you load the boot ...
user avatar
  • 2,200
3 votes

How to spawn the cubes on the terrain surface even if there is a hill on the terrain then spawn the cubes on the hill surface?

You don't really need the Terrain.SampleHeight() function, since hit.point already contains the information (i.e. the position ...
user avatar
  • 3,904
3 votes
Accepted

Rectangle collision not working!

We can make this a bit simpler using Minkowski addition. We can shrink one rectangle down to its center point, and enlarge the other rectangle from its center by the first's width and height, and get ...
user avatar
  • 112k
3 votes

Rectangle collision not working!

Preface The answer from @DMGregory has some wonderful math that will most likely solve most if not all of your problems. While I would recommend working towards an implementation like theirs, I ...
user avatar
3 votes
Accepted

Regarding performance, when is it okay and and when is it not okay to use LINQ with Unity?

You can't generalize things like that, because they always require context. A LINQ expression can include a lot of different operations, and operate on very different quantities of data. Any LINQ ...
user avatar
  • 111k
3 votes
Accepted

How to make mouse pointer be the vertex of a drawn parabola when estimating the trajectory of a projectile?

I think we can do this more simply. This demo uses about 80 lines of code (the interesting ones being just the dozen below), instead of over 300, with no angles/trigonometry functions needed: At the ...
user avatar
  • 112k
3 votes

How to not cover up the Android/iOS top status bar (eg. with time, network/wifi icons) in mobile Unity app?

I wrote this for a project a few years ago. It should work fine, but I no longer maintain it. It shows the status bar in the color of your choice. Usage: ...
user avatar
  • 5,161
3 votes

OpenTK immediate mode on net core doesn't work?

OpenTK is discouraging the use of immediate mode. But it is still supported. To use it, you need to set NativeWindowSettings.Profile to ...
user avatar
  • 17.9k
3 votes

Dash Cool Down Problem Unity2D

I say this only because this code is not yours: this code is horrific. 😱 I would not use any code from this source. Some of the problems: Comparing against / adding offsets to ...
user avatar
  • 112k
3 votes
Accepted

Forcing animations to play in real time, ignoring time scale

So for those who have the same question, I've found what needs to be modified: On the animator Component, there is a drop-down menu named "Update Mode" which has three options: Normal (in-...
user avatar
  • 599

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