Hi everyone. I’m getting very annoyed and tired of this now as I’ve been looking at this for 3 hours and still haven’t solved it. Here is the error I’m getting ‘NullReferenceException: Object reference not set to an instance of an object
PlayerMovement+c__Iterator8.MoveNext () (at Assets/Scripts/PlayerMovement.cs:280)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
PlayerMovement:Update() (at Assets/Scripts/PlayerMovement.cs:54)’
Quite long isn’t it? I have no idea why I’m getting it though. This here is the PlayerMovement script where the error points to.
public class PlayerMovement : MonoBehaviour {
public Camera cameraScript;
public AudioSource playerMovement;
public Button greenArrow;
public Button blueArrow;
public Button yellowArrow;
public Button redArrow;
public float pos;
private float rot;
private float x;
private float z;
public static bool playerMoved;
public static string lastMoveDir;
public static string changeYBy;
public static bool canMove;
public static int moves;
public Text movesText;
private void Start()
{
moves = 0;
playerMoved = false;
canMove = true;
pos = 0;
rot = 0;
}
private void Update()
{
transform.eulerAngles = new Vector3(x, transform.rotation.y, z);
if (movesText != null)
{
movesText.text = moves.ToString();
}
if(changeYBy == "Up")
{
StartCoroutine(MoveYUp());
changeYBy = "";
}
if (changeYBy == "Down")
{
StartCoroutine(MoveYDown());
changeYBy = "";
}
}
private void OnTriggerEnter(Collider col)
{
if (col.tag == "Chapter1Bullet")
{
Chapter1Boss.hitPlayer = true;
}
}
public void LeftButton()
{
if (GameMaster.endTheLevel == false && canMove == true && Environment.environmentReady == true)
{
moves = moves + 1;
playerMoved = true;
StartCoroutine(MoveLeft());
StartCoroutine(RotateLeft());
lastMoveDir = "Left";
}
}
public void RightButton()
{
if (GameMaster.endTheLevel == false && canMove == true && Environment.environmentReady == true)
{
moves = moves + 1;
playerMoved = true;
StartCoroutine(MoveRight());
StartCoroutine(RotateRight());
lastMoveDir = "Right";
}
}
public void UpButton()
{
if (GameMaster.endTheLevel == false && canMove == true && Environment.environmentReady == true)
{
moves = moves + 1;
playerMoved = true;
StartCoroutine(MoveUp());
StartCoroutine(RotateUp());
lastMoveDir = "Up";
}
}
public void DownButton()
{
if (GameMaster.endTheLevel == false && canMove == true && Environment.environmentReady == true)
{
moves = moves + 1;
playerMoved = true;
StartCoroutine(MoveDown());
StartCoroutine(RotateDown());
lastMoveDir = "Down";
}
}
IEnumerator MoveLeft()
{
Camera.enableArrows = false;
greenArrow.interactable = false;
blueArrow.interactable = false;
yellowArrow.interactable = false;
redArrow.interactable = false;
pos = this.transform.position.z - 0.4f;
while (this.transform.position.z > pos)
{
this.transform.position = this.transform.position + new Vector3(0f, 0f, -0.06f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, pos);
pos = 0;
yield return new WaitForSeconds(0.05f);
Camera.enableArrows = true;
greenArrow.interactable = true;
blueArrow.interactable = true;
yellowArrow.interactable = true;
redArrow.interactable = true;
}
IEnumerator RotateLeft()
{
playerMovement.Play();
z = 0;
rot = x - 90f;
while (x > rot)
{
x = x - 6f;
yield return new WaitForSeconds(0.007f);
}
x = rot;
rot = 0;
}
IEnumerator MoveUp()
{
Camera.enableArrows = false;
greenArrow.interactable = false;
blueArrow.interactable = false;
yellowArrow.interactable = false;
redArrow.interactable = false;
pos = this.transform.position.x - 0.4f;
while (this.transform.position.x > pos)
{
this.transform.position = this.transform.position + new Vector3(-0.06f, 0f, 0f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(pos, this.transform.position.y, this.transform.position.z);
pos = 0;
yield return new WaitForSeconds(0.05f);
Camera.enableArrows = true;
greenArrow.interactable = true;
blueArrow.interactable = true;
yellowArrow.interactable = true;
redArrow.interactable = true;
}
IEnumerator RotateUp()
{
playerMovement.Play();
x = 0;
rot = z + 90f;
while (z < rot)
{
z = z + 6f;
yield return new WaitForSeconds(0.007f);
}
z = rot;
rot = 0;
}
IEnumerator MoveRight()
{
Camera.enableArrows = false;
greenArrow.interactable = false;
blueArrow.interactable = false;
yellowArrow.interactable = false;
redArrow.interactable = false;
pos = this.transform.position.z + 0.4f;
while (this.transform.position.z < pos)
{
this.transform.position = this.transform.position + new Vector3(0f, 0f, 0.06f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(this.transform.position.x, this.transform.position.y, pos);
pos = 0;
yield return new WaitForSeconds(0.05f);
Camera.enableArrows = true;
greenArrow.interactable = true;
blueArrow.interactable = true;
yellowArrow.interactable = true;
redArrow.interactable = true;
}
IEnumerator RotateRight()
{
playerMovement.Play();
z = 0;
rot = x + 90f;
while (x < rot)
{
x = x + 6f;
yield return new WaitForSeconds(0.007f);
}
x = rot;
rot = 0;
}
IEnumerator MoveDown()
{
Camera.enableArrows = false;
greenArrow.interactable = false;
blueArrow.interactable = false;
yellowArrow.interactable = false;
redArrow.interactable = false;
pos = this.transform.position.x + 0.4f;
while (this.transform.position.x < pos)
{
this.transform.position = this.transform.position + new Vector3(0.06f, 0f, 0f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(pos, this.transform.position.y, this.transform.position.z);
pos = 0;
yield return new WaitForSeconds(0.05f);
Camera.enableArrows = true;
greenArrow.interactable = true;
blueArrow.interactable = true;
yellowArrow.interactable = true;
redArrow.interactable = true;
}
IEnumerator RotateDown()
{
playerMovement.Play();
x = 0;
rot = z - 90f;
while (z > rot)
{
z = z - 6f;
yield return new WaitForSeconds(0.007f);
}
z = rot;
rot = 0;
}
IEnumerator MoveYUp()
{
Camera.enableArrows = false;
greenArrow.interactable = false;
blueArrow.interactable = false;
yellowArrow.interactable = false;
redArrow.interactable = false;
pos = this.transform.position.y + 0.4f;
while (this.transform.position.y < pos)
{
this.transform.position = this.transform.position + new Vector3(0f, 0.06f, 0f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(this.transform.position.x, pos, this.transform.position.z);
pos = 0;
yield return new WaitForSeconds(0.05f);
if (lastMoveDir == "Left")
{
LeftButton();
}
if (lastMoveDir == "Right")
{
RightButton();
}
if (lastMoveDir == "Up")
{
UpButton();
}
if (lastMoveDir == "Down")
{
DownButton();
}
}
IEnumerator MoveYDown()
{
Camera.enableArrows = false;
greenArrow.interactable = false;
blueArrow.interactable = false;
yellowArrow.interactable = false;
redArrow.interactable = false;
pos = this.transform.position.y - 0.4f;
while (this.transform.position.y > pos)
{
this.transform.position = this.transform.position - new Vector3(0f, 0.06f, 0f);
yield return new WaitForSeconds(0.005f);
}
this.transform.position = new Vector3(this.transform.position.x, pos, this.transform.position.z);
pos = 0;
yield return new WaitForSeconds(0.05f);
Camera.enableArrows = true;
greenArrow.interactable = true;
blueArrow.interactable = true;
yellowArrow.interactable = true;
redArrow.interactable = true;
}
}
The error appears on line 280, which is the ‘greenArrow.interactable = false;’ inside the MoveYUp coroutine. greenArrow and the others are buttons, so why does this throw an error. And yes, the buttons are linked up correctly in Unity.
Also the weird thing is is that the error only starts appearing in these buttons when the ‘MoveYUp’ coroutine is started. Everything is fine before that.
If someone could give me some advice before I quit Unity for life that would be great lol
Thanks everyone!