- Home /
C# Class to struct error
Hi there!
I'm modifying a script that handles blocks for a voxel world generator. Things run a bit slow, so I'm changing the former Block class to a struct.
See what follows:
public struct Block
{
private static int _minLight = 45;
public static int MinLight { get { return _minLight; } }
private static int _maxLight = 255;
public static int MaxLight { get { return _maxLight; } }
public BlockType _type;
public BlockType Type { get { return _type; } set { _type = value; } }
private int _light;
public int Light
{
get { return _light; }
set
{
int light = Mathf.Clamp(value, 0, _maxLight);
if (_light != light)
{
_light = light;
if (_chunk != null)
_chunk.SetDirty();
}
}
}
private Chunk _chunk;
public Chunk Chunk { get { return _chunk; } set { _chunk = value; } }
public int _wx;
public int _wy;
public int _wz;
public Block(BlockType type, int wx = 0, int wy = 0, int wz = 0)
{
_wx = wx;
_wy = wy;
_wz = wz;
_type = type;
_light = 0;
if (_type == BlockType.Height)
{
_light = _maxLight;
}
else if (_type == BlockType.Bright)
{
_light = _maxLight;
}
else
{
_light = 0;
}
}
// Those methods looks up what class should serve this request, and forwards it
// Note: remember this was supposed to be a struct...
public bool IsSolid()
{
return BlockTypes.GetBlockType(_type).IsSolid(this);
}
public Vector2 TextureBottomUV()
{
return BlockTypes.GetBlockType(_type).TextureBottomUV(this);
}
public Vector2 TextureTopUV()
{
return BlockTypes.GetBlockType(_type).TextureTopUV(this);
}
public Vector2 TextureSideUV()
{
return BlockTypes.GetBlockType(_type).TextureSideUV(this);
}
public Vector2 TextureUV()
{
return BlockTypes.GetBlockType(_type).TextureUV(this);
}
public bool Destroy()
{
return BlockTypes.GetBlockType(_type).Destroy(this);
}
public bool Create(BlockType type)
{
return BlockTypes.GetBlockType(_type).Create(this, type);
}
}
Now, you probably didn't need ALL that to solve this, but hopefully I gave you enough to help me. The error I get is:
Field Block._chunk must be fully assigned before control leaves the inspector
Any ideas? I tried looking some stuff up online, but it was pretty confusing :P If there's a quick fix to this I would greatly appreciate any help. If there's not a quick fix to this (as in it is longer and more complicated than what's on the surface to fix everything) I would appreciate if someone would tell me that as well :)
Thanks!
-SS
Answer by hiddenspring81 · May 31, 2013 at 01:28 AM
It's actually pretty simple; if you create a struct
, you you need to assign values of all of the object's fields inside of your constructor. In this case, it appears that you haven't assigned a value to the chunk
field inside your constructor. You should modify your constructor so that an appropriate value can be supplied,
public Block(Chunk chunk, BlockType type, int wx = 0, int wy = 0, int wz = 0)
{
_chunk = chunk;
// Everything else goes here
}
However, I don't really think that you're going to see any performance benefit from switching to a struct. Structs and classes are two very, very different things in C#. Typically, you choose classes by default, and you only use structs to model new primitive types, such as an int
, float
, bool
, etc. Structs have all kinds of consequences, such as they cannot be subclassed, they are pass-by-value by default, and their should always be designed to have immutable state.
Thank you so much! Yeah, I think that perhaps I'll just stick with the class for now ;) I was going off of something found here : http://www.blockstory.net/node/59 But I'm not sure I know enough about C# to be able to reasonably convert everything. Thanks anyway for your quick response, and I think it did work!
-Stopsecret
Your answer
![](https://koobas.hobune.stream/wayback/20220613103230im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Distribute terrain in zones 3 Answers
Illuminating a 3D object's edges OnMouseOver (script in c#)? 1 Answer
Returning a struct to another script 1 Answer