Search Unity

object reference is required but only inside a switch

Discussion in 'Scripting' started by BlaM666, Jul 6, 2019.

  1. BlaM666

    BlaM666

    Joined:
    Mar 27, 2019
    Posts:
    6
    When i try to run my program i get an "An object reference is required for the non-static field, method, or property 'MonoBehaviour.StartCoroutine(IEnumerator)'" error


    Code (CSharp):
    1. public static void DayEvent()
    2.     {
    3.         rand = Random.Range(1, 10);
    4.         switch (rand)
    5.         {
    6.             default:
    7.                 StartCoroutine(BuyFood()); //Error on this line
    8.                 break;
    9.         }
    10.     }
    but BuyFood is definitely static:

    Code (CSharp):
    1. public static IEnumerator BuyFood()
    2.     {
    3.         answered = false;
    4.         question = "Would you like to buy food?";
    5.         PlayerManager.answer = 0;
    6.         TextManager.ChangeText(question);
    7.         yield return new WaitUntil(() => answered == true);
    8.         Debug.Log("waitover");
    9.         if (PlayerManager.answer == 1)
    10.         {
    11.             if (PlayerCharacter.player.Gold > 5)
    12.             {
    13.                 PlayerCharacter.player.Hunger = +1;
    14.                 PlayerCharacter.player.Gold -= 5;
    15.                 TextManager.ChangeText("You bought food");
    16.                 PlayerManager.answer = 0;
    17.             }
    18.             else
    19.             {
    20.                 TextManager.ChangeText("You do not have enough money");
    21.                 PlayerManager.answer = 0;
    22.             }
    23.         }
    24.         else if (PlayerManager.answer == -1)
    25.         {
    26.             TextManager.ChangeText("You didn't buy food");
    27.             PlayerManager.answer = 0;
    28.         }
    29.         yield return new WaitForSeconds(5);
    30.         TextManager.ChangeText("");
    31.     }
    ive been able to run this before by calling it like that before, its just when i moved it into a switch it stopped working, any suggestions?
     
  2. tonemcbride

    tonemcbride

    Joined:
    Sep 7, 2010
    Posts:
    1,089