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. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Object reference not set to an instance of an object?

Discussion in 'Scripting' started by EpicGoldenNinja, May 22, 2018.

  1. EpicGoldenNinja

    EpicGoldenNinja

    Joined:
    Feb 8, 2018
    Posts:
    4
    I have made this visual novel but there's this error that keeps appearing. "NullReferenceException: Object reference not set to an instance of an object" Usually, you can't run games with error but I can run it with this error and works like its suppose to. It works in exe too. However, it just does not work in WebGL which is the format I want it in. Please help! The code is below. PS: The error is in the displayImage and displayText code.
    Code (CSharp):
    1. using System.Collections;
    2. using System.Collections.Generic;
    3. using UnityEngine;
    4. using UnityEngine.UI;
    5. using UnityEngine.SceneManagement;
    6.  
    7.  
    8.  
    9. public enum GameState
    10. {
    11.     rightnowend, menu, Livingroom, school, bedroom, retard, black, Livingroom1, Livingroom2, Livingroom3, Livingroom4, Livingroom5, Livingroom6, Livingroom7, Livingroom8, Livingroom9, Livingroom10, Livingroom11, Livingroom12, Path1_1, Path1_2, Path1_3, Path1_4, Path1_5, Path1_6, Path1_7, Livingroom13, Livingroom14, Livingroom15, Livingroom16, Livingroom17, Livingroom18, Path2_1, Path2_2, Path2_3, Path2_4, Path2_5, Path2_6, Path3_1, Path3_2, Path3_3, Path3_4
    12. }
    13.  
    14. public class GameController : MonoBehaviour
    15. {
    16.  
    17.     public string levelname;
    18.     public Text displayText;
    19.     public Image displayImage;
    20.     public Text displayText2;
    21.     public Image displayImage2;
    22.     int ending = 0;
    23.     public enum GameState
    24.     {
    25.         rightnowend, menu, Livingroom, school, bedroom, retard, black, Livingroom1, Livingroom2, Livingroom3, Livingroom4, Livingroom5, Livingroom6, Livingroom7, Livingroom8, Livingroom9, Livingroom10, Livingroom11, Livingroom12, Path1_1, Path1_2, Path1_3, Path1_4, Path1_5, Path1_6, Path1_7, Livingroom13, Livingroom14, Livingroom15, Livingroom16, Livingroom17, Livingroom18,
    26.         Path2_2,
    27.         Path3_1,
    28.         Path2_1,
    29.         Path2_3,
    30.         Path2_4,
    31.         Path2_5,
    32.         Path2_6,
    33.         Path2_7,
    34.         Path2_8,
    35.         Path2_9,
    36.         Path2_10,
    37.         Path3_2,
    38.         endfornow
    39.     }
    40.     public GameState currentGameState;
    41.  
    42.  
    43.  
    44.     // Use this for initialization
    45.     void Start()
    46.     {
    47.         currentGameState = GameState.menu;
    48.         displayImage.sprite = Resources.Load<Sprite>("Black");
    49.         displayText2.text = "Me";
    50.         displayImage2.sprite = Resources.Load<Sprite>("Blank");
    51.        
    52.     }
    53.  
    54.  
    55.     // Update is called once per frame
    56.     void Update()
    57.     {
    58.  
    59.         if (Input.GetKeyDown(KeyCode.R))
    60.         {
    61.             SceneManager.LoadScene(levelname);
    62.         }
    63.    
    64.  
    65.         if (currentGameState == GameState.menu) { // Check if the current game state is Start
    66.             displayImage.sprite = Resources.Load<Sprite> ("Black");
    67.             displayText.text = "Welcome to the game. \n\n Press Mouse 1 to go to the next scene, R to restart.";
    68.  
    69.             if (Input.GetButtonDown ("Fire1")) { // Check if the player has pressed X
    70.                 currentGameState = GameState.black; // Change the current game state to Forest
    71.             }
    72.         } else if (currentGameState == GameState.black) {
    73.             displayText.text = "Finally, I finished my 2 year exchange program. I wonder how my Sister, Ivy, is doing.";
    74.             if (Input.GetButtonDown ("Fire1")) {
    75.                 currentGameState = GameState.Livingroom;
    76.  
    77.             }
    78.        
    79.         } else if (currentGameState == GameState.Livingroom) {
    80.             displayText.text = "*Reaches home*";
    81.             displayImage.sprite = Resources.Load<Sprite>("BG2");
    82.             displayText2.text = " ";
    83.             if (Input.GetButtonDown ("Fire1")) {
    84.                 currentGameState = GameState.Livingroom2;
    85.  
    86.             }
    87.  
    88.  
    89.         } else if (currentGameState == GameState.Livingroom2) {
    90.             displayText.text = "I'm bac...";
    91.             displayText2.text = "Me";
    92.             if (Input.GetButtonDown ("Fire1")) {
    93.                 currentGameState = GameState.Livingroom3;
    94.  
    95.             }
    96.         } else if (currentGameState == GameState.Livingroom3) {
    97.             displayText.text = "AHH! \n\n ";
    98.             displayText2.text = "???";
    99.             if (Input.GetButtonDown ("Fire1")) {
    100.                 currentGameState = GameState.Livingroom4;
    101.             }
    102.         } else if (currentGameState == GameState.Livingroom4) {
    103.             displayText.text = "Um... What happened?";
    104.             displayText2.text = "Me";
    105.             if (Input.GetButtonDown ("Fire1")) {
    106.                 currentGameState = GameState.Livingroom5;
    107.             }
    108.  
    109.         } else if (currentGameState == GameState.Livingroom5) {
    110.             displayText.text = "That sounded like Ivy! I should check on her!";
    111.             displayText2.text = "???";
    112.             if (Input.GetButtonDown ("Fire1")) {
    113.                 currentGameState = GameState.Livingroom6;
    114.             }
    115.  
    116.         } else if (currentGameState == GameState.Livingroom6) {
    117.             displayText.text = "IVY!";
    118.             displayText2.text = "Me";
    119.             displayImage.sprite = Resources.Load<Sprite>("BG");
    120.             if (Input.GetButtonDown ("Fire1")) {
    121.                 currentGameState = GameState.Livingroom7;
    122.             }
    123.  
    124.         } else if (currentGameState == GameState.Livingroom7) {
    125.             displayText.text = "We...Welcome back!";
    126.             displayText2.text = "???";
    127.             displayImage2.sprite = Resources.Load<Sprite> ("Ivy");
    128.             if (Input.GetButtonDown ("Fire1")) {
    129.                 currentGameState = GameState.Livingroom8;
    130.             }
    131.  
    132.         } else if (currentGameState == GameState.Livingroom8) {
    133.             displayText.text = "This is my sister, Ivy.";
    134.             displayText2.text = " ";
    135.             displayImage2.sprite = Resources.Load<Sprite> ("Ivy");
    136.             if (Input.GetButtonDown ("Fire1")) {
    137.                 currentGameState = GameState.Livingroom9;
    138.             }
    139.  
    140.         } else if (currentGameState == GameState.Livingroom9) {
    141.             displayText.text = "Uh yeah.";
    142.             displayText2.text = "Me";
    143.             displayImage2.sprite = Resources.Load<Sprite> ("Ivy");
    144.             if (Input.GetButtonDown ("Fire1")) {
    145.                 currentGameState = GameState.Livingroom10;
    146.             }
    147.  
    148.         } else if (currentGameState == GameState.Livingroom10) {
    149.             displayText.text = "So, what happened just now?";
    150.             displayText2.text = "Me";
    151.             displayImage2.sprite = Resources.Load<Sprite> ("Ivy");
    152.             if (Input.GetButtonDown ("Fire1")) {
    153.                 currentGameState = GameState.Livingroom11;
    154.             }
    155.  
    156.         } else if (currentGameState == GameState.Livingroom11) {
    157.             displayText.text = "No...Nothing happened!";
    158.             displayText2.text = "Ivy";
    159.             displayImage2.sprite = Resources.Load<Sprite> ("IvyNervz");
    160.             if (Input.GetButtonDown ("Fire1")) {
    161.                 currentGameState = GameState.Livingroom12;
    162.             }
    163.  
    164.         } else if (currentGameState == GameState.Livingroom12) {
    165.             displayText.text = "Choice: 1 to ask about screaming, 2 to Ask more about the cut, 3 to trust her. (Will become buttons in next release) ";
    166.             displayText2.text = "";
    167.             displayImage2.sprite = Resources.Load<Sprite> ("IvyNervz");
    168.             if (Input.GetKeyDown ("1")) {
    169.                 currentGameState = GameState.Path1_1;
    170.                 ending = ending + 0;
    171.             }
    172.             if (Input.GetKeyDown ("2")) {
    173.                 currentGameState = GameState.Path2_1;
    174.                 ending = ending - 1;
    175.             }
    176.             if (Input.GetKeyDown ("3")) {
    177.                 currentGameState = GameState.Path3_1;
    178.                 ending = ending + 1;
    179.             }
    180.  
    181.         } else if (currentGameState == GameState.Path1_1) {
    182.             displayText.text = "Why did you scream?";
    183.             displayText2.text = "Me";
    184.             displayImage2.sprite = Resources.Load<Sprite> ("IvyNervz");
    185.             if (Input.GetButtonDown ("Fire1")) {
    186.                 currentGameState = GameState.Path1_2;
    187.             }
    188.  
    189.         } else if (currentGameState == GameState.Path1_2) {
    190.             displayText.text = "I just cut my hand while I was doing art homework!";
    191.             displayText2.text = "Ivy";
    192.             displayImage2.sprite = Resources.Load<Sprite> ("IvyNervz");
    193.             if (Input.GetButtonDown ("Fire1")) {
    194.                 currentGameState = GameState.Path1_3;
    195.             }
    196.         } else if (currentGameState == GameState.Path1_3) {
    197.             displayText.text = "I was trying to cut a piece of paper but accidentally cut myself...";
    198.             displayText2.text = "Ivy";
    199.             displayImage2.sprite = Resources.Load<Sprite> ("IvyNervz");
    200.             if (Input.GetButtonDown ("Fire1")) {
    201.                 currentGameState = GameState.Path1_4;
    202.             }
    203.  
    204.         } else if (currentGameState == GameState.Path1_4) {
    205.             displayText.text = "Thats an awful lot of blood for a cut!";
    206.             displayText2.text = "Me";
    207.             displayImage2.sprite = Resources.Load<Sprite> ("IvyNervz");
    208.             if (Input.GetButtonDown ("Fire1")) {
    209.                 currentGameState = GameState.Path1_5;
    210.             }
    211.         } else if (currentGameState == GameState.Path1_5) {
    212.             displayText.text = "Go clean up then, I'll go make lunch.";
    213.             displayText2.text = "Ivy";
    214.             displayImage2.sprite = Resources.Load<Sprite> ("IvyNervz");
    215.             if (Input.GetButtonDown ("Fire1")) {
    216.                 currentGameState = GameState.Path1_6;
    217.             }
    218.         } else if (currentGameState == GameState.Path1_6) {
    219.             displayText.text = "Thanks!";
    220.             displayText2.text = "Ivy";
    221.             displayImage2.sprite = Resources.Load<Sprite> ("Ivy");
    222.             if (Input.GetButtonDown ("Fire1")) {
    223.                 currentGameState = GameState.Livingroom13;
    224.             }
    225.  
    226.         } else if (currentGameState == GameState.Path2_1) {
    227.             displayText.text = "Be honest. What happened?";
    228.             displayText2.text = "Me";
    229.             displayImage2.sprite = Resources.Load<Sprite> ("IvyNervz");
    230.             if (Input.GetButtonDown ("Fire1")) {
    231.                 currentGameState = GameState.Path2_2;
    232.             }
    233.         } else if (currentGameState == GameState.Path2_2) {
    234.             displayText.text = "What do you mean? I-it's just a cut!";
    235.             displayText2.text = "Ivy";
    236.             displayImage2.sprite = Resources.Load<Sprite> ("IvyNervz");
    237.             if (Input.GetButtonDown ("Fire1")) {
    238.                 currentGameState = GameState.Path2_3;
    239.             }
    240.  
    241.         } else if (currentGameState == GameState.Path2_3) {
    242.             displayText.text = "How do you explain the cut?";
    243.             displayText2.text = "Me";
    244.             displayImage2.sprite = Resources.Load<Sprite> ("IvyFruz");
    245.             if (Input.GetButtonDown ("Fire1")) {
    246.                 currentGameState = GameState.Path2_4;
    247.  
    248.  
    249.             }
    250.  
    251.         } else if (currentGameState == GameState.Path2_4) {
    252.             displayText.text = "Like I said! A cut!";
    253.             displayText2.text = "Ivy";
    254.             displayImage2.sprite = Resources.Load<Sprite> ("IvyFruz");
    255.             if (Input.GetButtonDown ("Fire1")) {
    256.                 currentGameState = GameState.Path2_5;
    257.             }
    258.  
    259.         } else if (currentGameState == GameState.Path2_5) {
    260.             displayText.text = "Thats too much blood for a small cut! Tell me the truth!";
    261.             displayText2.text = "Me";
    262.             displayImage2.sprite = Resources.Load<Sprite> ("IvyFruz");
    263.             if (Input.GetButtonDown ("Fire1")) {
    264.                 currentGameState = GameState.Path2_6;
    265.             }
    266.  
    267.         } else if (currentGameState == GameState.Path2_6) {
    268.             displayText.text = "I am telling the truth!";
    269.             displayText2.text = "Ivy";
    270.             displayImage2.sprite = Resources.Load<Sprite> ("IvyFruz");
    271.             if (Input.GetButtonDown ("Fire1")) {
    272.                 currentGameState = GameState.Path2_7;
    273.             }
    274.  
    275.         } else if (currentGameState == GameState.Path2_7) {
    276.             displayText.text = "Show me your hand! I need to see what really happened!";
    277.             displayText2.text = "Me";
    278.             displayImage2.sprite = Resources.Load<Sprite> ("IvyFruz");
    279.             if (Input.GetButtonDown ("Fire1")) {
    280.                 currentGameState = GameState.Path2_8;
    281.             }
    282.  
    283.         } else if (currentGameState == GameState.Path2_8) {
    284.             displayText.text = "Theres nothing! Leave me alone! *Pushes MC*";
    285.             displayText2.text = "Ivy";
    286.             displayImage2.sprite = Resources.Load<Sprite> ("IvyFruz");
    287.             if (Input.GetButtonDown ("Fire1")) {
    288.                 currentGameState = GameState.Path2_9;
    289.             }
    290.  
    291.         } else if (currentGameState == GameState.Path2_9) {
    292.             displayText.text = "Thats weird... \n I guess I'll just brush it aside for now...";
    293.             displayText2.text = "Ivy";
    294.             displayImage2.sprite = Resources.Load<Sprite> ("IvyFruz");
    295.             if (Input.GetButtonDown ("Fire1")) {
    296.                 currentGameState = GameState.Livingroom13;
    297.             }
    298.         } else if (currentGameState == GameState.Path3_1)
    299.                     {
    300.                         displayText.text = "Thats quite a deep cut huh.";
    301.                         displayText2.text = "Ivy";
    302.                         displayImage2.sprite = Resources.Load<Sprite>("IvyNervz");
    303.                         if (Input.GetButtonDown("Fire1"))
    304.                         {
    305.                             currentGameState = GameState.Path3_2;
    306.                         }
    307.  
    308.                     }
    309.                     else if (currentGameState == GameState.Path3_2)
    310.                     {
    311.                         displayText.text = "Go clean up, I'll go get a plaster. \n*She must be hiding something!* ";
    312.                         displayText2.text = "Me";
    313.                         displayImage2.sprite = Resources.Load<Sprite>("IvyNervz");
    314.                         if (Input.GetButtonDown("Fire1"))
    315.                         {
    316.                             currentGameState = GameState.Livingroom13;
    317.                         }
    318.  
    319.                     }
    320.                     else if (currentGameState == GameState.Livingroom13)
    321.                     {
    322.                         displayText.text = "Anyway, have you eaten lunch yet?";
    323.                         displayText2.text = "Me";
    324.                         displayImage2.sprite = Resources.Load<Sprite>("Ivy");
    325.                         if (Input.GetButtonDown("Fire1"))
    326.                         {
    327.                             currentGameState = GameState.Livingroom14;
    328.                         }
    329.  
    330.                     }
    331.                     else if (currentGameState == GameState.Livingroom14)
    332.                     {
    333.                         displayText.text = "No... Not yet";
    334.                         displayText2.text = "Ivy";
    335.                         displayImage2.sprite = Resources.Load<Sprite>("Ivy");
    336.                         if (Input.GetButtonDown("Fire1"))
    337.                         {
    338.                             currentGameState = GameState.Livingroom15;
    339.                         }
    340.  
    341.                     }
    342.                     else if (currentGameState == GameState.Livingroom15)
    343.                     {
    344.                         displayText.text = "I'll go make lunch then.";
    345.                         displayText2.text = "Me";
    346.                         displayImage2.sprite = Resources.Load<Sprite>("Ivy");
    347.                         if (Input.GetButtonDown("Fire1"))
    348.                         {
    349.                             currentGameState = GameState.Livingroom16;
    350.                         }
    351.  
    352.                     }
    353.                     else if (currentGameState == GameState.Livingroom16)
    354.                     {
    355.                         displayText.text = "Thanks!";
    356.                         displayText2.text = "Ivy";
    357.                         displayImage2.sprite = Resources.Load<Sprite>("Ivy");
    358.                         if (Input.GetButtonDown("Fire1"))
    359.                         {
    360.                             currentGameState = GameState.Livingroom17;
    361.                         }
    362.  
    363.                     }
    364.                     else if (currentGameState == GameState.Livingroom17)
    365.                     {
    366.                         displayText.text = "END OF CHAPTER 1 \n\n Chapter 2: The story actually begins. \n Will be included if this gets past qualifiers.";
    367.                         displayText2.text = "Preview";
    368.                         displayImage2.sprite = Resources.Load<Sprite>("Blank");
    369.                         if (Input.GetButtonDown("Fire1"))
    370.                         {
    371.                 currentGameState = GameState.Livingroom18;
    372.                         }
    373.  
    374.                     }
    375.         else if (currentGameState == GameState.Livingroom18)
    376.         {
    377.             displayText.text = "What to expect for the next update: Mini Games, OST, even more STORY and ALL 3 ENDINGS.";
    378.             displayText2.text = "Preview";
    379.             displayImage2.sprite = Resources.Load<Sprite>("Blank");
    380.             if (Input.GetButtonDown("Fire1"))
    381.             {
    382.                 currentGameState = GameState.endfornow;
    383.             }
    384.  
    385.         }
    386.                 }
    387.             }
    388.        
     
  2. johne5

    johne5

    Joined:
    Dec 4, 2011
    Posts:
    1,133
    i'm pretty sure it's because webgl can't load resource from a local path
     
  3. EpicGoldenNinja

    EpicGoldenNinja

    Joined:
    Feb 8, 2018
    Posts:
    4
    Huh? What does that mean? The other people could run theirs fine. Or do I not put the sprites in folder?
     
  4. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    You say "It works like it's suppose to", but "it just does not work in WebGL" ... So, which is it?

    If your error is with displayText and displayImage, but they appear to be working when you run them, you most likely have a duplicate of the script in your scene somewhere.
     
  5. EpicGoldenNinja

    EpicGoldenNinja

    Joined:
    Feb 8, 2018
    Posts:
    4
    It works in the game tab for Unity and Exe. But it comes up with an error when run on WebGL.
     

    Attached Files:

  6. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,140
    Then again, make sure you don't have a duplicate script. Otherwise, look at your javascript console and see what it says. (which isn't always helpful, but sometimes)
     
  7. EpicGoldenNinja

    EpicGoldenNinja

    Joined:
    Feb 8, 2018
    Posts:
    4
    I
    I'll try. Thanks.