Null Reference Exeption??
So as some might know, I am making a minecraft command block as a project for school and I am getting a Null reference exeption for a random reason. I checked my whole scene to see if any thing had my script that had no assined vars and its clear! So is it in my script??
#pragma strict
//The string that is edited in the text field
var Texture1 : Texture2D;
var Texture2 : Texture2D;
var Texture3 : Texture2D;
var Impulse : boolean;
var Chain : boolean;
var Repeting : boolean;
public var stringToEdit : String;
var creativeMode : flyControllerScript;
var survivalMode : MonoBehaviour;
//The command as entered by the player
public var command : String;
public var command2 : String;
public var command3 : String;
public var command4 : String;
var rigidbdy : Rigidbody;
//Boolean to track when the player clicks to enable the text field
public var clicked : boolean;
//Boolean to track when the player has confirmed the command to enter
public var shouldCheckCommand : boolean;
function Start()
{
survivalMode.enabled = true;
creativeMode.enabled = false;
//Initialize variables
stringToEdit = "/type in here";
clicked = false;
shouldCheckCommand = false;
}
function OnMouseDown ()
{
stringToEdit = "/type in here";
clicked = true;
}
function Update()
{
//Check for the mouse click - the text field is invisible
//if(Input.GetMouseButtonDown (0) && clicked == false)
//{
//Reset the string to default
// stringToEdit = "/type in here";
//clicked = true;
//}
//Check for the Return/Enter key - the text field is visible
if (Input.GetKeyDown(KeyCode.Return) && clicked == true)
{
command = stringToEdit; //Here is the key part - the string from the text field is assigned to the command variable
command2 = stringToEdit; //Here is the key part - the string from the text field is assigned to the command variable
clicked = false;
shouldCheckCommand = true;
}
//Check if the player has confirmed the command entered
if (shouldCheckCommand == true)
{
CheckCommand();
}
}
function CheckCommand()
{
//Check which command was entered and take the appropriate action
if (command == "/kill" || command == "/Kill")
{
Debug.Log("Kill the player");
Application.LoadLevel(Application.loadedLevel);
if(Repeting == true)
{
Debug.Log("Kill the player");
Application.LoadLevel(1);
}
}
shouldCheckCommand = false;
//Check which command was entered and take the appropriate action
if (command2 == "/help"|| command2 == "/Help")
{
Debug.Log("The list of commands: /gamemode 0 (change to survival) /gamemode 1 (change to creative) /kill (kills player) /help (list of commands) More Commands comming soon");
while (Repeting == true)
{
Debug.Log("The list of commands: /gamemode 0 (change to survival) /gamemode 1 (change to creative) /kill (kills player) /help (list of commands) More Commands comming soon");
}
}
shouldCheckCommand = false;
if (command3 == "/gamemode 0"|| command3 == "/gamemode survival")
{
Debug.Log("Gamemode is now survival");
creativeMode = GetComponent(flyControllerScript);
survivalMode.enabled = true;
creativeMode.enabled = false;
rigidbdy.GetComponent.<Rigidbody>().isKinematic = true;
rigidbdy.GetComponent.<Rigidbody>().detectCollisions = true;
if (Repeting == true)
{
Debug.Log("Warning Code has protected unity from crashing!");
}
}
shouldCheckCommand = false;
if (command4 == "/gamemode 1"|| command4 == "/gamemode creative")
{
Debug.Log("Gamemode is now Creative");
creativeMode = GetComponent(flyControllerScript);
creativeMode.enabled = true;
survivalMode.enabled = false;
rigidbdy.GetComponent.<Rigidbody>().isKinematic = true;
rigidbdy.GetComponent.<Rigidbody>().detectCollisions = false;
if (Repeting == true)
{
Debug.Log("Warning Code has protected unity from crashing!");
}
}
shouldCheckCommand = false;
}
function OnGUI()
{
if(clicked == true)
{
stringToEdit = GUI.TextField (Rect (0, 100, 200, 200), stringToEdit, 200);
GUI.Button(Rect(0, 0, 200, 50), "Command Block Interface");
if (GUI.Button(Rect(0, 300, 200, 50), "Impulse")){
GetComponent.<Renderer>().material.mainTexture = Texture1;
Impulse = true;
}
if (GUI.Button(Rect(0, 350, 200, 50), "Chain")){
GetComponent.<Renderer>().material.mainTexture = Texture2;
Chain = true;
}
if (GUI.Button(Rect(0, 400, 200, 50), "Repeting")){
GetComponent.<Renderer>().material.mainTexture = Texture3;
Repeting = true;
}
if (GUI.Button(Rect(0, 500, 200, 50), "Done")){
clicked = false;
}
}
}
Please help !
Where is the error taking place? The console will tell you. For example, this mockup says the error is on line 21
NullReferenceException: Object reference not set to an instance of an object
$$anonymous$$yScript.Start () (at Assets/$$anonymous$$yScript.cs:21)
creative$$anonymous$$ode is null. This can happen when GetComponent can't find the component. Does the GameObject have a flyControllerScript attached to it?
Then something else is wrong. flyControllerScript was a guess, since creative$$anonymous$$ode mode is on line 103. One of your objects is losing a reference, or a GetComponent is failing to return. I wonder if the problem lies within flyControllerScript itself.
This is what the error is NullReferenceException: Object reference not set to an instance of an object CommandBlockAI+$CheckCommand$6+$.$$anonymous$$oveNext () (at Assets/Redstone/CommandBlock/Scripts/CommandBlockAI.js:103) UnityEngine.$$anonymous$$onoBehaviour:StartCoroutine_Auto(IEnumerator) CommandBlockAI:Update() (at Assets/Redstone/CommandBlock/Scripts/CommandBlockAI.js:62)
Here is the flyControllerScript
var lookSpeed = 15.0; var moveSpeed = 15.0;
var rotationX = 0.0; var rotationY = 0.0;
function Update () { rotationX += Input.GetAxis("$$anonymous$$ouse X")*lookSpeed; rotationY += Input.GetAxis("$$anonymous$$ouse Y")*lookSpeed; rotationY = $$anonymous$$athf.Clamp (rotationY, -90, 90);
transform.localRotation = Quaternion.AngleAxis(rotationX, Vector3.up);
transform.localRotation *= Quaternion.AngleAxis(rotationY, Vector3.left);
transform.position += transform.forward*moveSpeed*Input.GetAxis("Vertical");
transform.position += transform.right*moveSpeed*Input.GetAxis("Horizontal");
}
Your answer
Follow this Question
Related Questions
Component refrence goes null randomly? 3 Answers
I don't understand NullReferenceException: Object reference not set to an instance of an object 0 Answers
NullReferenceException: Object reference not set to an instance of an object error 0 Answers
Coroutine Issue 1 Answer
I have a NullReferenceException problem how do I fix this error 0 Answers