• Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
  • Help Room /
avatar image
0
Question by boyyprince · Apr 03, 2017 at 02:07 AM · nullreferenceexception

NullReferenceException: Object reference not set to an instance of an object

Okay I'm having this problem, and I'm not sure how to fix it!

I'm following this tutorial to make a visual novel. I'm a complete beginner! http://www.indiana.edu/~gamedev/2015/09/27/creating-a-visual-novel-in-unity/

I'm struggling with the part where I've just made my Character prefab (it contains my static character and all their poses), but they won't appear in the scene. I've even tried dragging the prefab into the scene directly. It even won't appear in the gameplay box. The layer orders are correct, and I'm assuming the reason they're not showing up must have to do with the error message I have:

 NullReferenceException: Object reference not set to an instance of an object
 DialogueManager.Start () (at Assets/Scripts/DialogueManager.cs:29)

And these are the codes I'm working with:

Dialogue Parser:

using UnityEngine; using System.Collections; using UnityEditor; using System.Text; using System.IO; using System.Text.RegularExpressions; using System.Collections.Generic;

public class DialogueParser : MonoBehaviour {

 struct DialogueLine {
     public string name;
     public string content;
     public int pose;
     public string position;
     public string[] options;

     public DialogueLine(string Name, string Content, int Pose, string Position) {
         name = Name;
         content = Content;
         pose = Pose;
         position = Position;
         options = new string[0];
     }
 }

 List<DialogueLine> lines;

 // Use this for initialization
 void Start () {
     string file = "Assets/Data/Dialogue";
     string sceneNum = EditorApplication.currentScene;
     sceneNum = Regex.Replace (sceneNum, "[^0-9]", "");
     file += sceneNum;
     file += ".txt";

     lines = new List<DialogueLine>();

     LoadDialogue (file);
 }

 // Update is called once per frame
 void Update () {

 }

 void LoadDialogue(string filename) {
     string line;
     StreamReader r = new StreamReader (filename);

     using (r) {
         do {
             line = r.ReadLine();
             if (line != null) {
                 string[] lineData = line.Split(';');
                 if (lineData[0] == "Player") {
                     DialogueLine lineEntry = new DialogueLine(lineData[0], "", 0, "");
                     lineEntry.options = new string[lineData.Length-1];
                     for (int i = 1; i < lineData.Length; i++) {
                         lineEntry.options[i-1] = lineData[i];
                     }
                     lines.Add(lineEntry);
                 } else {
                     DialogueLine lineEntry = new DialogueLine(lineData[0], lineData[1], int.Parse(lineData[2]), lineData[3]);
                     lines.Add(lineEntry);
                 }
             }
         }
         while (line != null);
         r.Close();
     }
 }

 public string GetPosition(int lineNumber) {
     if (lineNumber < lines.Count) {
         return lines[lineNumber].position;
     }
     return "";
 }

 public string GetName(int lineNumber) {
     if (lineNumber < lines.Count) {
         return lines[lineNumber].name;
     }
     return "";
 }

 public string GetContent(int lineNumber) {
     if (lineNumber < lines.Count) {
         return lines[lineNumber].content;
     }
     return "";
 }

 public int GetPose(int lineNumber) {
     if (lineNumber < lines.Count) {
         return lines[lineNumber].pose;
     }
     return 0;
 }

 public string[] GetOptions(int lineNumber) {
     if (lineNumber < lines.Count) {
         return lines[lineNumber].options;
     }
     return new string[0];
 }

}

Dialogue Manager:

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.Collections.Generic;
 
 public class DialogueManager : MonoBehaviour {
 
     DialogueParser parser;
 
     public string dialogue, characterName;
     public int lineNum;
     int pose;
     string position;
     string[] options;
     public bool playerTalking;
     List<Button> buttons = new List<Button> ();
 
     public Text dialogueBox;
     public Text nameBox;
     public GameObject choiceBox;
 
     // Use this for initialization
     void Start () {
         dialogue = "";
         characterName = "";
         pose = 0;
         position = "L";
         playerTalking = false;
         parser = GameObject.Find("DialogueParser").GetComponent<DialogueParser>();
         lineNum = 0;
     }
 
     // Update is called once per frame
     void Update () {
         if (Input.GetMouseButtonDown (0) && playerTalking == false) {
             ShowDialogue();
 
             lineNum++;
         }
 
         UpdateUI ();
     }
 
     public void ShowDialogue() {
         ResetImages ();
         ParseLine ();
     }
 
     void UpdateUI() {
         if (!playerTalking) {
             ClearButtons();
         }
         dialogueBox.text = dialogue;
         nameBox.text = characterName;
     }
 
     void ClearButtons() {
         for (int i = 0; i < buttons.Count; i++) {
             print ("Clearing buttons");
             Button b = buttons[i];
             buttons.Remove(b);
             Destroy(b.gameObject);
         }
     }
 
     void ParseLine() {
         if (parser.GetName (lineNum) != "Player") {
             playerTalking = false;
             characterName = parser.GetName (lineNum);
             dialogue = parser.GetContent (lineNum);
             pose = parser.GetPose (lineNum);
             position = parser.GetPosition (lineNum);
             DisplayImages();
         } else {
             playerTalking = true;
             characterName = "";
             dialogue = "";
             pose = 0;
             position = "";
             options = parser.GetOptions(lineNum);
             CreateButtons();
         }
     }
 
     void CreateButtons() {
         for (int i = 0; i < options.Length; i++) {
             GameObject button = (GameObject)Instantiate(choiceBox);
             Button b = button.GetComponent<Button>();
             ChoiceButton cb = button.GetComponent<ChoiceButton>();
             cb.SetText(options[i].Split(':')[0]);
             cb.option = options[i].Split(':')[1];
             cb.box = this;
             b.transform.SetParent(this.transform);
             b.transform.localPosition = new Vector3(0,-25 + (i*50));
             b.transform.localScale = new Vector3(1, 1, 1);
             buttons.Add (b);
         }
     }
 
     void ResetImages() {
         if (characterName != "") {
             GameObject character = GameObject.Find (characterName);
             SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
             currSprite.sprite = null;
         }
     }
 
     void DisplayImages() {
         if (characterName != "") {
             GameObject character = GameObject.Find(characterName);
 
             SetSpritePositions(character);
 
             SpriteRenderer currSprite = character.GetComponent<SpriteRenderer>();
             currSprite.sprite = character.GetComponent<Character>().characterPoses[pose];
         }
     }
 
 
     void SetSpritePositions(GameObject spriteObj) {
         if (position == "L") {
             spriteObj.transform.position = new Vector3 (-6, 0);
         } else if (position == "R") {
             spriteObj.transform.position = new Vector3 (6, 0);
         }
         spriteObj.transform.position = new Vector3 (spriteObj.transform.position.x, spriteObj.transform.position.y, 0);
     }
 }


I hope this is enough information to work with, but I can post anything else that's needed! Please let me know if anyone knows how I can fix this problem. :)

I'm sure it must be a simple fix, but I'm so new to all of this...I'm very overwhelmed by everything @_@

Thank you!

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

0 Replies

· Add your reply
  • Sort: 

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

99 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

NullReferenceException, Script Error 1 Answer

GetComponent returns null 1 Answer

NullReferenceException 0 Answers

Reference lost in NetworkEntity 0 Answers

NullReferenceException while raycasting? 0 Answers


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges