0

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);
}
PetqImaQkaputka
  • 137
  • 1
  • 7
  • where have you initialized _items? i think because of that it might be giving a null reference exception – Sujit.Warrier Jul 01 '16 at 11:44
  • @Mysterio11 in the field initializer – Marc Gravell Jul 01 '16 at 11:46
  • what is the stacktrace on the exception? what line is throwing? that's kinda what we (and you) need to know, and it is all there in the exception – Marc Gravell Jul 01 '16 at 11:47
  • 1
    because AllStats inherits form IEnumerable, try replacing with **AllStats : Stats** – Miller Jul 01 '16 at 11:52
  • Hi @PetqImaQkaputka. You're going to have real problems until you come to terms with the fact that Unity *simply does not use the latest c#*. – Fattie Jul 01 '16 at 11:53
  • A bigger, huge, problem you face is that you think Unity is OO. Unity is *not even slightly* OO. Unity is like using say Photoshop or Microsoft Word. This is a huge problem which programmers with some experience face in using Unity for the first time. This essay may help. http://stackoverflow.com/a/37243035/294884 Or, you may just ignore it and continue along another path. – Fattie Jul 01 '16 at 11:57
  • Do you have any good graphics? What's the logo for the game? – Fattie Jul 01 '16 at 12:20
  • Is it more first person shooter or is this an arcade-type ? – Fattie Jul 01 '16 at 12:20
  • is there a chance you could add a small screen shot or even just a shot of a similar existing game? i'm not really sure which game you're referring to as an example. that sounds like an excellent name. – Fattie Jul 01 '16 at 12:26
  • what backend are you using (I mean like Parse, Firebase, Gamesparks) – Fattie Jul 01 '16 at 12:26
  • you don't show your class `Stats` so it's hard to help on this question – Fattie Jul 01 '16 at 12:28
  • i would just edit, click the "image" thing and throw in a couple small images of the games you reference. it's difficult to see how you could proceed with such a game without the backend? (you're TOTALLY ON THE MONEY making a "maker" game where the user creates weapons, etc.) note that the system you use (Firebase, whatever) would "answer that question" you know??? – Fattie Jul 01 '16 at 12:50
  • hi @LasseV.Karlsen (1) if you think that's the case click the Close button (2) if you have the time, pls tell where you think the null reference is coming up. (3) I've indeed already asked the OP to explain what GameObject the script is attached to and how it works in the scene – Fattie Jul 01 '16 at 12:51
  • 1
    All the questions/comments about *what the game actually is* is irrelevant, please concentrate on the actual question as the comment thread becomes really noisy. – Lasse V. Karlsen Jul 01 '16 at 12:51
  • 1
    it's impossible to have a clue about what the problem might be unless in general terms we know what the heck is going on in the game. anyways I'm off for now, bye! – Fattie Jul 01 '16 at 12:52

1 Answers1

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 .....

enter image description here

enter image description here

... 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!)

Fattie
  • 30,632
  • 54
  • 336
  • 607