-1

New to Unity, working on an infectious disease simulation. I have a "patient" class to which I want to assign a "disease". To start, I just want to be able to print the patient's disease to console on spawn. How do I go about this? Here's my current attempt:

public class Disease : MonoBehaviour
{
    public string[] diseases = new string[] {"Pneumonia", "Influenza", "Covid-19"};
}

public class Patient : MonoBehaviour
{
    public Disease disease;
    void Start()
    {   
        disease = Disease.diseases[Random.Range(0, diseases.Length)];
        Debug.Log("Patient has: " + disease);
    }
}

I get these errors:

  1. error CS0120: An object reference is required for the non-static field, method, or property 'Disease.diseases'
  2. error CS0103: The name 'diseases' does not exist in the current context

Thanks.

  • A doubt here... Are the Disease and Patient in two different scripts? – IcyThug Sep 28 '21 at 11:32
  • Yes, sorry I didnt specify. They are two different scripts. – Seth Peters Sep 28 '21 at 12:06
  • Does this answer your question: [CS0120: An object reference is required for the nonstatic field, method, or property 'foo'](https://stackoverflow.com/questions/498400/cs0120-an-object-reference-is-required-for-the-nonstatic-field-method-or-prop) ? – derHugo Sep 28 '21 at 13:31
  • @SethPeters If that's the case.. I guess my answer below should help you out!! – IcyThug Sep 28 '21 at 18:17
  • @derHugo Yes it does, but only after I looked at the other answers on my post. The code in your link is too complex for me at the moment to decipher the problem or realize the answer, but I do now understand that "static" is required to access a variable from somewhere else. – Seth Peters Sep 28 '21 at 18:44
  • `but I do now understand that "static" is required to access a variable from somewhere else` no, then you didn't really understand yet .... `static` means something doesn't belong to a certain instance of a type but rather directly to the type itself. The error is telling you that your stuff is non-static and therefore requires an instance of that type in order to use it ... Which is exactly what is better described and explained in the duplicate link's accepted answer including what options you have ... – derHugo Sep 28 '21 at 19:50
  • @IcyThug it doesn't matter at all in c# whether two types are within the same script file or not (except they are in different assemblies but that's another topic) ... – derHugo Sep 28 '21 at 19:54

2 Answers2

0

You are trying to reach an object property through its class as if it is a static property. Change your diseases property in Disease class to be static and also change this line to this, because you forgot to type Disease:

// In Disease class
public static string[] diseases = new string[] {"Pneumonia", "Influenza", "Covid-19"};

// In patient class
disease = Disease.diseases[Random.Range(0, Disease.diseases.Length)];

Also you cant assign that value to the disease because its an Disease object and value this line returns is a string so change disease to be a string like this:

public class Patient : MonoBehaviour {

    public string disease;
    ...
}

Note : You can also think about removing MonoBehaviour from Disease class if you are not thinking about attaching it into a GameObject.

shrain
  • 38
  • 5
  • Thanks this worked perfectly. The "static" variable was the exact behaviour I was looking for. Do you know if this is the best practice for this type of thing or is there a method that experienced developers prefer? – Seth Peters Sep 28 '21 at 18:41
  • I'm glad that it helped, I'll be really happy if you can mark my answer as accepted one. When it comes to best practices it depends on what you are trying to achieve with the Disease class, but if you're not planning to attach it to a GameObject I think it's not necessary for it to inherit MonoBehaviour. Static classes and properties are not bad practice so there is nothing to worry about. – shrain Sep 28 '21 at 18:59
0

If you have these code in two different scripts and have attached them to a gameobject the following solution will work:

Patient:

public class Patient : MonoBehaviour
{
    public Disease disease;
    public string dis;
    void Start()
    {
        dis = disease.diseases[Random.Range(0, disease.diseases.Length)];
        Debug.Log("Patient has: " + dis);
    }
}

Disease:

public class Disease : MonoBehaviour
{
    public string[] diseases = new string[] { "Pneumonia", "Influenza", "Covid-19" };
}

But this doesn't work if you don't attach to gameObject because you obviously need to attach it so the start function works... So this is what i did: Created an empty gameObject -> put both your scripts in it -> referenced Dicease script into patient script by drag and dropping in inspector -> And the changes in code thats there in the answer -> the code works.

IcyThug
  • 76
  • 8
  • Thank you, it worked; although I am trying to avoid having to attach multiple scripts in the inspector (I only want script "Patient" on my patient object so I dont get confused), therefore I used the method in shrain's answer below. – Seth Peters Sep 28 '21 at 18:47
  • @SethPeters Yeah, no problem! I'm happy to help... Why I kept them in a separate script was just in case if you do more things in each class at one-point it gets clumsy and unreadable. So kept them this way... But its fine you do it in the same class too.. Have a nice day :D – IcyThug Sep 29 '21 at 04:37