Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question Set base class variable instance to derived class instance

Discussion in 'Scripting' started by jmaslaki, Sep 23, 2023.

  1. jmaslaki

    jmaslaki

    Joined:
    Jul 28, 2018
    Posts:
    5
    bold = class names
    italic = instance of class name

    So I have another class with a variable called

    Code (CSharp):
    1. INHERIT who;
    I know it's not a conventionally named class but whatever, it's mine and I like it

    But I have more classes, NPC inherits INHERIT, and opponent inherits NPC

    From another class I wanna set who equal to an instance of opponent(named enemy), but the problem is who is an INHERIT, and it doesn't work in code, but it works in the inspector, but I can't do it in the inspector

    If I write
    Code (CSharp):
    1. HP.who = enemy;
    No errors, but when I look in the inspector during runtime, it's null

    Obviously, I don't wanna make a opponent who and have to write a bunch of duplicate code for that, so how do I set INHERIT who to opponent enemy?
     
  2. orionsyndrome

    orionsyndrome

    Joined:
    May 4, 2014
    Posts:
    3,043
    Look, I don't want to brush up on your ego, but you're too much of a cowboy for someone who hasn't really mastered the underlying system. What do you expect will happen? Not only you're not engaging in a proper conversation here, but you're also introducing a detriment to your learning strategy, by intentionally doing things lopsided from everyone else.

    These standards aren't really a matter of us being blind followers, it's because as the code gets more complex (and it inevitably always does unless you're making a FizzBuzz example), it is useful to have clear and easily-discernible patterns that cognitively comply with a vast amount of people, typically people who've invested years into analyzing and navigating such code with proficiency, and have developed deep cognitive habits. Which you simply ruin by doing things "your way". It's not about freedom, it's about communication.

    How would you feel if I used my Slavic mother language to reply to you? Would you be interested in further communication?

    In other words: if you can fix your attitude, then we can perhaps fix your code, and I can guarantee you that you will level up in your understanding.
     
    Bunny83 likes this.
  3. Bunny83

    Bunny83

    Joined:
    Oct 18, 2010
    Posts:
    3,525
    If that's really the case, the assignment would work just fine since, as you said "enemy" is of type "opponent" and therefore also an "NPC" and also by extend an "INHERIT". So you said

    So that's not really possible. Since you said that it "doesn't work" in code, it also means the compiler complained about your code with a clear error message which you haven't shared with us. So what does the compiler say about that assignment line you mentioned? It would also help to actually show the relevant code snippet. My guess is that "enemy" is NOT of type opponent but maybe of type GameObject, Component or something else ?

    Without clear information we can't provide clear answers.

    ps: I would also highly recommend to stick to some basic coding rules. It's not necessary to follow strict rules, however aligning with some of the most basic styles (and more importantly, be consistent) is important to not shoot yourself in the foot in the long term.
     
    orionsyndrome likes this.