Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question initialize a variable with different classes

Discussion in 'Scripting' started by Saw9yer, Jan 16, 2022.

  1. Saw9yer

    Saw9yer

    Joined:
    Sep 19, 2020
    Posts:
    72
    Ineed some help for a problem i have since long time.

    I use a function to display answers in a dialogue system.
    My player can interact with a Character, an object....

    My answers are all stored in an object (dialoguesStoring) that contain many classes :
    -Class DialoguesCharactersStoring contain my answers when i speak to a character
    -Class DialogueObjectsStoring contain my answers when i speak to an object

    In the code bellow my problem is that i don't know how to initialize my variable scr because i need to choose the right class according to the interlocutor.

    Code (CSharp):
    1. void DisplayAnswers()
    2.     {
    3.         var scr;
    4.  
    5.         if (tagInterlocutor_string == "Character")
    6.         {
    7.             scr = dialoguesStoring.GetComponent<DialoguesCharactersStoring>();
    8.         }
    9.         else if (tagInterlocutor_string == "object")
    10.         {
    11.             scr = dialoguesStoring.GetComponent<DialogueObjectsStoring>();
    12.         }
    13.  
    14.         if (answersAmount == 1)
    15.         {
    16.             answer1.GetComponent<AnswersDisplay>().active = true;
    17.             answer1.GetComponent<AnswersDisplay>().answer = scr.dialogueAnswer[nameInterlocutor_int, line, 1];
    18.         }
    19.         else if (answersAmount == 2)
    20.         {
    21.             answer1.GetComponent<AnswersDisplay>().active = true;
    22.             answer2.GetComponent<AnswersDisplay>().active = true;
    23.             answer1.GetComponent<AnswersDisplay>().answer = scr.dialogueAnswer[nameInterlocutor_int, line, 1];
    24.             answer2.GetComponent<AnswersDisplay>().answer = scr.dialogueAnswer[nameInterlocutor_int, line, 2];
    25.  
    26.         }
    27.         else if (answersAmount == 3)
    28.         {
    29.             answer1.GetComponent<AnswersDisplay>().active = true;
    30.             answer2.GetComponent<AnswersDisplay>().active = true;
    31.             answer3.GetComponent<AnswersDisplay>().active = true;
    32.  
    33.             answer1.GetComponent<AnswersDisplay>().answer = scr.dialogueAnswer[nameInterlocutor_int, line, 1];
    34.             answer2.GetComponent<AnswersDisplay>().answer = scr.dialogueAnswer[nameInterlocutor_int, line, 2];
    35.             answer3.GetComponent<AnswersDisplay>().answer = scr.dialogueAnswer[nameInterlocutor_int, line, 3];
    36.         }
    37.         else
    38.         {
    39.             ResetAnswers();
    40.         }      
     
  2. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    The compiler just needs you to initialize it to SOMETHING, even null.

    The pattern I like for these things is to assume something safe, or something debuggy, like a stub dialog that tells you when you forgot to set tagInterlocultor_string to something valid.
     
  3. SisusCo

    SisusCo

    Joined:
    Jan 29, 2019
    Posts:
    1,295
    You can create a base class from which all of your different Dialogues*Storing components derive from.

    Code (CSharp):
    1. public abstract class DialoguesStoring : MonoBehaviour
    2. {
    3.     DialogueAnswerDatabase dialogueAnswer;
    4. }
    5.  
    6. public class DialoguesCharactersStoring : DialoguesStoring { }
    7.  
    8. public class DialogueObjectsStoring : DialoguesStoring { }
    Then you can assign an instance of any of these components into a variable whose type matches the base class type.

    Code (CSharp):
    1.        DialoguesStoring scr;
    2.  
    3.         if (tagInterlocutor_string == "Character")
    4.         {
    5.             scr = dialoguesStoring.GetComponent<DialoguesCharactersStoring>();
    6.         }
    7.         else if (tagInterlocutor_string == "object")
    8.         {
    9.             scr = dialoguesStoring.GetComponent<DialogueObjectsStoring>();
    10.         }
     
  4. Saw9yer

    Saw9yer

    Joined:
    Sep 19, 2020
    Posts:
    72
    Yes i already tried without sucess
     
  5. Kurt-Dekker

    Kurt-Dekker

    Joined:
    Mar 16, 2013
    Posts:
    38,514
    Success will continue to be elusive until you say exactly what the error is.

    I told you how to initialize it. Set it to null. If that's not the error, then start here:

    The complete error message contains everything you need to know to fix the error yourself.

    The important parts of the error message are:

    - the description of the error itself (google this; you are NEVER the first one!)
    - the file it occurred in (critical!)
    - the line number and character position (the two numbers in parentheses)
    - also possibly useful is the stack trace (all the lines of text in the lower console window)

    Always start with the FIRST error in the console window, as sometimes that error causes or compounds some or all of the subsequent errors. Often the error will be immediately prior to the indicated line, so make sure to check there as well.

    All of that information is in the actual error message and you must pay attention to it. Learn how to identify it instantly so you don't have to stop your progress and fiddle around with the forum.

    Remember: NOBODY here memorizes error codes. That's not a thing. The error code is absolutely the least useful part of the error. It serves no purpose at all. Forget the error code. Put it out of your mind.
     
    Saw9yer likes this.
  6. Saw9yer

    Saw9yer

    Joined:
    Sep 19, 2020
    Posts:
    72
    For exemple with that :
    Code (CSharp):
    1.         var scr = null;
    2.  
    3.         if (tagInterlocutor_string == "Character")
    4.         {
    5.             scr = dialoguesStoring.GetComponent<DialoguesCharactersStoring>();
    6.         }
    7.         else if (tagInterlocutor_string == "object")
    8.         {
    9.             scr = dialoguesStoring.GetComponent<DialogueObjectsStoring>();
    10.         }
    I have CS0815 : Cannot assign <null> to an implicity-typed variable.
     
  7. MelvMay

    MelvMay

    Unity Technologies

    Joined:
    May 24, 2013
    Posts:
    11,316
    Did you see the post here?

    You need to look-up what "var" means. It can be used when the compiler can infer what that statement is assigning. You are not assigning a type so it has no idea. You have to specify a concrete type if you're not assigning a type and the link above is the answer if you're going to assign two different types.

    In your code, what is assigned is done at runtime. The compiler works at compile time. Var isn't a runtime thing that can be assigned anything, it's a compile-time thing to save a little typing (syntactic sugar) is all. It's not a "variant" type or something else you'd find in another language.

    Either that or rework your execution flow here.

    https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/keywords/var