Suggested workaround OOP Design w/ Unity bugs ?

Ok, this is kinda bugging me lately in the regards that I can't use my standard OOP design techniques with unity. How do others deal with complicated class hiearchies with Unity 3 ?

As a concrete example I propose this weapon system:

I have a class called GunData, which holds info about a particular gun, fire rate, recoil, muzzle velocity, etc... Along with modifier data, shoulder state data, and secondary gun id's.

using UnityEngine;
using System.Collections;
using System.Collections.Generic;

[System.Serializable]
public class GunData
{
    public string Name = "Unnamed";

    public float FireRate = 0.05f;
    public float Recoil = 1.0f;
    //...

    public int GunID;
    public Transform GunMesh;
    public GunData ShoulderData;
    public GunType WeaponType = GunType.Bullet;
    public int SecondaryGunID;

    public GunAttachment[] AttachmentData;

    public enum GunType
    {
        Rockets,
        Energy,
        Bullet,
        Rail,
        Explosives,
        Placement
    }
}

The first bug I find is that when I have a 2nd class, like GunAttachment with a different enum but a variable with the same name, the editor is using the wrong enum definition for GunData.

As in this does not work:

[System.Serializable]
public class GunAttachment 
{
    public GunData Modifier;
    public AttachmentType WeaponType;

    public enum AttachmentType
    {
        Scope,
        Stock,
        Handle,
        Clip,
        Silencer,
        Attachment
    }
}

But renaming the variable WeaponType to SomethingElse lets it work completely even though the enums are completely different in different classes.

The 2nd issue I am finding is that I can't have a public reference to my own class. In this example the variable for ShoulderData does not show up in the inspector. My guess is that it would cause a stack blow out if unity creates memory for this, but I am intending it to be a reference to a GunData instance.

The real question I have is how do you design classes around this ? Do you make a 2nd class with all the same variables?

Just a question, are these simplified re-written versions of your classes or was this copy-pasted directly?

1 Answer

1

First, a question. Are you used to developing C# in the CLR or Mono? Because most of your problems seem to be caused by the different complier. Also, what version of Unity are you using? 3.0 has a Mono 2.6.7 (aka C# 3.5) compiler and runtime that may fix your problems. I am using Unity 3, and I cannot replicate issue 2, and #1 seems to be fine too.

Also, use a typeof test, rather than the inspector, to see what type the vaules are. The inspector is not a good debug tool.

I would just avoid using the conflicting name. A good one would be WeaponAttachmentType, or somthing similar. The editor is not important. The parser (in this case) is. The CLR uses a different code parser than the Mono, and the parser changed from the versions. This might have solved your problem if you were on 2.6.7, but since you are in 3.0, it is not a problem. Can you post the WeaponType enum? I tried your code, and it seems fine to me.

Also, do not assume that the inspector is a good representation of the actual object. It has trouble beyond 2 or 3 levels, so use a check script to make sure that there really is nothing there.