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?
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.
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.
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.