Can't instantiate new class C# [duplicate]

孤者浪人 提交于 2019-12-25 06:34:01

问题


I'm using the newest version of unity and I have a class with property public AllStats BaseStats { get; set; }, where the class AllStats is implemented like this:

public class AllStats : IEnumerable<Stats>
{
    private MagicPower magicPower;
    public MagicPower MagicPower
    {
        get { return magicPower; }
        set
        {
            magicPower = value;
            Add("Magic Power", magicPower);
        }
    }

    private DebuffDuration debuffDuration;
    public DebuffDuration DebuffDuration
    {
        get { return debuffDuration; }
        set
        {
            debuffDuration = value;
            Add("Debuff Duration", debuffDuration);
        }
    }

    private Defense defense;
    public Defense Defense
    {
        get { return defense; }
        set
        {
            defense = value;
            Add("Defense", defense);
        }
    }

    private MagicDefense magicDefense;
    public MagicDefense MagicDefense
    {
        get { return magicDefense; }
        set
        {
            magicDefense = value;
            Add("Magic Defense", magicDefense);
        }
    }

    private CriticalPower criticalPower;
    public CriticalPower CriticalPower
    {
        get { return criticalPower; }
        set
        {
            criticalPower = value;
            Add("Critical Power", criticalPower);
        }
    }

    private CriticalChance criticalChance;
    public CriticalChance CriticalChance
    {
        get { return criticalChance; }
        set
        {
            criticalChance = value;
            Add("Critical Chance", criticalChance);
        }
    }

    private Multistrike multistrike;
    public Multistrike Multistrike
    {
        get { return multistrike; }
        set
        {
            multistrike = value;
            Add("Multistrike", multistrike);
        }
    }

    private CooldownReduction cooldownReduction;
    public CooldownReduction CooldownReduction
    {
        get { return cooldownReduction; }
        set
        {
            cooldownReduction = value;
            Add("Cooldown Reduction", cooldownReduction);
        }
    }

    private Evasion evasion;
    public Evasion Evasion
    {
        get { return evasion; }
        set
        {
            evasion = value;
            Add("Evasion", evasion);
        }
    }

    private Resistance resistance;
    public Resistance Resistance
    {
        get { return resistance; }
        set
        {
            resistance = value;
            Add("Resistance", resistance);
        }
    }

    private readonly Dictionary<string, Stats> _items = new Dictionary<string, Stats>();

    public void Add(string element, Stats config)
    {
        _items[element] = config;
    }

    public Stats this[string element]
    {
        get { return _items.ContainsKey(element) ? _items[element] : null; }

        set { _items[element] = value; }
    }

    public IEnumerator<Stats> GetEnumerator()
    {
        return _items.Values.GetEnumerator();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return _items.Values.GetEnumerator();
    }
}

However, when I try to create new instance of this class like this :

BaseStats = new AllStats()
        {
            MagicPower = new MagicPower(Level.CurrentLevel),
            DebuffDuration = new DebuffDuration(Level.CurrentLevel),
            Defense = new Defense(Level.CurrentLevel),
            MagicDefense = new MagicDefense(Level.CurrentLevel),
            CriticalPower = new CriticalPower(1),
            CriticalChance = new CriticalChance(1),
            Multistrike = new Multistrike(1),
            CooldownReduction = new CooldownReduction(1),
            Evasion = new Evasion(1),
            Resistance = new Resistance(1)
        };

I get null reference exception. Why is that ? This used to work in normal visual studio, but it seems like the older version of unity is causing some problems. How can I fix this ?

Here is the Stats Class :

    public enum StatsOrders
{
    MagicPower,
    DebuffDuration,
    Defense,
    MagicDefense,
    CriticalPower,
    CriticalChance,
    Multistrike,
    CooldownReduction,
    Evasion,
    Resistance
}

public abstract class Stats : IStats
{
    public double PointsInStats { get; set; }

    public bool IsSuccessfulHit(double baseStats)
    {
        return Increase(baseStats)*10 >= new RngCrypto().Next(1, 1001);
    }

    public double Increase(double baseInput)
    {
        return CalculateIncreament(baseInput, PointsInStats);
    }

    protected abstract double percentageIncrease { get; set; }

    protected Stats(double pointsInStats)
    {
        PointsInStats = pointsInStats;
    }

    protected double CalculateIncreament(double baseInput, double pointsInStats)
    {
        string a = (baseInput * (pointsInStats / percentageIncrease)).ToString("N1");
        return double.Parse(a);
    }
}

The enum is used whenever you level up you get a small window of notification which includes the updated stats and this enum helps keep them in order.

Also the IStats interface :

public interface IStats
{
    double PointsInStats { get; set; }
    bool IsSuccessfulHit(double baseStats);
    double Increase(double input);
}

回答1:


What you are doing, I may misunderstand but I just can't see any connection to Unity or a game engine.

Say you are dealing with, for example, "different varieties of projectile". Ok? Here's an example from a game with ~3m players. All you can possibly do in a game engine is have GameObject. There is nothing else, whatsoever, in a game engine.

There are no "objects", no "inheritance" - there's just nothing, at all, other than GameObject. (Which carry the transfor tensors, which integrate to the GPU chip, and the rendering cycle, and hence it's a game engine.)

There is absolutely nothing, whatsoever, in Unity other than "GameObject". Note that you cannot in any way, at all, modify, change, vary or indeed do anything - whatsoever - to GameObject. Unity is not even vaguely OO or structuring. Unity is as OO as, say, my kitchen table.

Don't forget too that game engines have utterly no connection to programming where you have concepts like "classes" that may or may not be "instantiated" and so on. The only thing in a game engine is a whole lot of GameObjects - those are actual, specific things.

(You could say they are "instantiated" but that would be entirely missing the point - there are only singletons in a game engine, there's no conceptual way you can have a "not instantiated" GameObject. It doesn't even make sense to say it. Say you are using Photoshop, and you put "a colored line" on the page. You wouldn't say "oh, that's an instantiated colored line" - it's just .. one of the colored lines in the image. All that photoshop is is colored lines.)

Anyway ... somewhere or other you're going to have your **model of each projectile", say there are 7 of them (bomb, bullet, arrow ... you get the idea).

Later you will duplicate and place these as needed during game play (to make a machine gun work - or whatever).

So you go "click" and make a game object, perhaps sitting it offscreen, and you name it "Bomb".

OK, so your job is done. Someone else, not you, who is drunk and artistic will come along and add things like "a rendered" or perhaps "an animation" or whatever to the "bomb", "arrow", etc.

You will (there's no other way) have it set up like this .....

... so that your boss or the creative director, or whoever, can set the values like "damage", "speed factor" and so on.

{Note. Unity - currently - happens to use a language, c#, for writing behaviours, which is an "OO language". That's great and, of course, you have to be a master of OO, to, write that language. Unity may well change to using a totally non-OO language (lisp or something). The fact that the language used, currently for writing behaviors, happens to be, an OO language, has absolutely no relation to the fact that unity has utterly no connection to OO, it is an ECS system ("It's like using Microsoft Word").}

So, regarding the code fragment you present, I can't see how it is germane in Unity. Sure, it's an interesting abstract question ("how does this work in the version of net which Unity happens to be currently using..." or whatever) but it is a long way off reservation, seems like a time waste (your business, of course!)



来源:https://stackoverflow.com/questions/38144526/cant-instantiate-new-class-c-sharp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!