Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 Jun 22
sparklines
Close Help
  • 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 22ecvvw · May 29, 2016 at 09:06 PM · error messagenullreferenceexceptionnullreference

Null error.

 using UnityEngine;
 using System.Collections;
 using UnityEngine.UI;
 using System.Collections.Generic;
 using System;
 
 public class TextController : MonoBehaviour {
 
     private bool bStart = true;
     private string instructions = "You have the following commands at your disposal:\n" +
         "*\tFirst Verse, Second Verse\n" +
         "*\tFirst Chorus, Second Chorus, Third Chorus\n" +
             "*\tFirst Bridge, Second Bridge";
     private Text text;  
     private LyricList lineList = new LyricList();
     private InputField inputField;
     private string newState;
     String s = null; 
 
 
     // Use this for initialization
     void Start () {
 
 
         // Get the text child object from the Canvas
         text = gameObject.GetComponent<Text> ();
         // Get the input field child object from the Canvas
         inputField = gameObject.GetComponentInChildren<InputField> ();
         //CreateAssetMenuAttribute on Lyric List
         lineList.CreateList ();
 
         text.text = ("Please reassemble the song lyrics!\n" + instructions + "\n\nStart guessing to begin!\n\n");
 
     
     }
 
     void Update(){
             // Check for input from the input field
         if (Input.GetKeyDown (KeyCode.Return)) {
             if (inputField.text != "") { // .trim
                     newState = inputField.text;
                     newState = newState.ToLower();
                     lineList.LyricSwitch (newState);
                     inputField.text = "";
                     // If we've entered something bStart needs to be switched to false to print the lyrics
                     if(bStart){
                         bStart = false;
                     }
                 }
             }
             // We've had text entered into the input field, now show the correct lyrics
             else if (!bStart) {
                 foreach (Lyric line in lineList.lLyrics) {
                     if (line.getState () == lineList.getCurrentState ()) {
                         text.text += line.getLyric ();
                         text.text += "\n\n" + instructions + "\n";
                     }
                     
                 }
             }
         // Make sure to set the Input Field to Active
         //inputField.ActivateInputField ();
     }
     
 
 }
 
 
 
 SCRIPT 2
 public class LyricList {
 
     public List<Lyric> lLyrics = new List<Lyric>();
     private Helpers.STATE curState = Helpers.STATE.chorus1;
     private string[] aCommands = {"first chorus", "second chorus", "third chorus", "first bridge", "second bridge", "first verse", "second verse", "third verse"};
     
     public string getCurrentState(){
         return curState.ToString();
     }
 
     // Use Present Lyric to the 
     public void LyricSwitch (string newState) {
 
         for (int i = 0; i < aCommands.Length; i++) {
             if(string.Compare(newState, aCommands[i], true) == 0)
                 break;
             else if(i == aCommands.Length-1)
                 newState = "";
         }
         switch (newState) {
         case "first chorus":
             curState = Helpers.STATE.chorus1;
             break;
             
         case "second chorus":
             curState = Helpers.STATE.chorus2;
             break;
             
         case "third chorus":
             curState = Helpers.STATE.chorus3;
             break;
             
         case "first bridge":
             curState = Helpers.STATE.bridge1;
             break;
             
         case "second bridge":
             curState = Helpers.STATE.bridge2;
             break;
             
         case "first verse":
             curState = Helpers.STATE.verse1;
             break;
             
         case "second verse":
             curState = Helpers.STATE.verse2;
             break;
             
         case "third verse":
             curState = Helpers.STATE.verse3;      
             break;
 
         default:
             curState = Helpers.STATE.notachoice;
             break;
             
         }
 
         
     }
 
     public void CreateList () {
         
         // Chorus lyrics
         lLyrics.Add ( new Lyric("chorus1", "Never gonna give you up\n"
                                 + "Never gonna let you down/n "));
         lLyrics.Add ( new Lyric("chorus2", "Never gonna run around and desert you\n"
                                 + "Never gonna make you cry\n"));
         lLyrics.Add ( new Lyric("chorus3", "Never gonna say goodbye\n"
                                 + "Never gonna tell a lie and hurt you\n\n"));
         
         // door scripts
         lLyrics.Add ( new Lyric("bridge1", "I just wanna tell you how I'm feeling\n"
                                 + "Gotta make you understand\n\n"));
         
         lLyrics.Add ( new Lyric("bridge2", "And if you ask me how I'm feeling\n"
                                 + "Don't tell me you're too blind to see\n\n"));
         
         // bowl scripts
         lLyrics.Add ( new Lyric("verse1", "We're no strangers to love\n"
                                 + "You know the rules and so do I\n"
                                 + "A full commitment's what I'm thinking of\n"
                                 + "You wouldn't get this from any other guy\n"));
         lLyrics.Add ( new Lyric("verse2", "We've known each other for so long\n"
                                 + "Your heart's been aching, but\n"
                                 + "You're too shy to say it\n"
                                 + "Inside, we both know what's been going on\n"
                                 + "We know the game and we're gonna play it\n"));
         lLyrics.Add ( new Lyric("notachoice", "That isn't an option."));
     }
 
 }
 
 
 SCRIPT 3
 public class Lyric {
 
     private Helpers.STATE eState;
     private string sLyric;
 
     public Lyric(string state, string script){
         eState = Helpers.ParseEnum<Helpers.STATE>(state);
         sLyric = script;
     }
 
     public string getLyric(){
         return sLyric;
     }
     
     public string getState(){
         return eState.ToString();
     }
 
 
     public void setLyric(string lyric){
         sLyric = lyric;
     }
 
     public void setState(string state){
         eState = Helpers.ParseEnum<Helpers.STATE>(state);
     }
 
     public bool compareStatetoString(string compare){
         if (eState == Helpers.ParseEnum<Helpers.STATE> (compare))
             return true;
         else
             return false;
     }
 
 
 
 }
 
 
 SCRIPT 4
 using UnityEngine;
 using System.Collections;
 
 public class Helpers {
 
     public enum STATE{chorus1, chorus2, chorus3, bridge1, bridge2, verse1, verse2, verse3, notachoice};
 
     public static T ParseEnum<T>(string value)
     {
         return (T) System.Enum.Parse(typeof(T), value, true);
     }
 
 }
 
 
 Null ref is on line 40 of the first script... it says an object is not set to an instance... I can not find the object and its really bugging me out. Any help?
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

1 Reply

· Add your reply
  • Sort: 
avatar image
0

Answer by Thajocoth · May 29, 2016 at 09:24 PM

Line 36 of the first script is empty in the above. Have you tried normalizing your line endings? Sometimes inconsistent line endings can cause an error to report the wrong line of the script as an error.

Looking near line 36 rather than at it... Is it possible that inputField is null? The call to GetComponentInChildren could be failing. If you attach the debugger and add some breakpoints, you could find out for sure.

Instead of using the GetComponent variants, you could make the variables public and set them in Unity's inspector. This will cause them to be set before Start() is ever even called and remove any doubt about whether or not they're null.

Comment
Add comment · Show 5 · Share
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
avatar image 22ecvvw · May 29, 2016 at 09:29 PM 0
Share

Line 40 my bad

avatar image Thajocoth 22ecvvw · May 29, 2016 at 09:35 PM 0
Share

Well, the only nullable on that line is inputField, so I'd say that inputField must be what's null. This probably means that GetComponentInChildren is failing. Is the TextController script on a GameObject that has a child that contains an InputField? This would be in the Unity inspector, not in the code.

avatar image 22ecvvw Thajocoth · May 29, 2016 at 09:37 PM 0
Share

But where could it possibly be? Like right now i have a canvas < text and my Text control is in that. When i try to move forward it breaks back to null.

Show more comments
Show more comments

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.

Follow this Question

Answers Answers and Comments

59 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

Related Questions

Object reference not set to an instance of an object - Jumping Scripts (C#) 1 Answer

NullReferenceException randomly occurs 2 Answers

ML Agents Null Pointer error 0 Answers

Why is my destroy platform script now working? NullReferenceException: Object reference not set to an instance of an object. 1 Answer

JS NullReferenceException: Object reference not set to an instance of an object 1 Answer


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