I say ‘Bizarre’ because until now I’ve been able to debug my issues relatively competently. The scenario-
When i play my scene normally from the inspector i have no issues at all. All co-routines function as normal. When i reload the level using application.loadlevel or load it from another scene i get the following error on multiple Co-routines (but not all)
NullReferenceException: Object reference not set to an instance of an object
Daniella+<Bounce>c__Iterator13.MoveNext () (at Assets/Resources/GAME2/Scripts/CutScenes/Daniella.cs:108)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
Daniella:RightStep() (at Assets/Resources/GAME2/Scripts/CutScenes/Daniella.cs:91)
<OM5Steps>c__Iterator34:MoveNext() (at Assets/Resources/GAME2/Scripts/Handlers/PlayerHandler.cs:595)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
PlayerHandler:CSONE() (at Assets/Resources/GAME2/Scripts/Handlers/PlayerHandler.cs:153)
PlayerHandler:OnLevelWasLoaded(Int32) (at Assets/Resources/GAME2/Scripts/Handlers/PlayerHandler.cs:1046)
I’ve included the full log as opposed to just the null reference exception because I’m absolutely lost as to what’s being called as null. The co-routine itself is part of a movement script, it bounces the rigidbody up and down of an NPC whilst it moves during a cut scene.
It plays 3 times successfully in spite of the error and then fails to perform the last movement step.
Here’s the relevant code snippets mentioned in the log, I’m listing them all because i can’t isolate the problem to any single part of it, the script on the NPC is IDENTICAL to several other NPC’s performing the IDENTICAL co-routine in the scene without any errors.
Daniella.cs:108
IEnumerator Bounce()
{
rb.velocity = new Vector3(0, 1, 0); //line 108
yield return new WaitForSeconds(0.2f);
rb.velocity = new Vector3(0, 0, 0);
}
Daniella.cs:91
public void RightStep()
{
pos += Vector3.right;
SoundHandler.instance.RandomizeEnemSfx(Move1, Move2);
StartCoroutine(Bounce()); // line 91
}
PlayerHandler (This script isn’t destroyed on load) cs:595
IEnumerator OM5Steps()
{
danScript.RightStep(); //line 595
yield return new WaitForSeconds(0.35f);
danScript.RightStep();
orphanScript.RightStep();
yield return new WaitForSeconds(0.35f);
danScript.RightStep();
orphanScript.RightStep();
yield return new WaitForSeconds(0.35f);
danScript.RightStep(); // this last step is never executed
orphanScript.RightStep();
yield return new WaitForSeconds(0.35f);
orphanScript.RightStep();
yield return new WaitForSeconds(0.35f);
orphanScript.RightStep();
yield return new WaitForSeconds(0.35f);
CSONE(); // this line is executed successfully in spite of the error
}
Playerhandler cs:153 is simply just starting the above co-routine in a method called CSONE()
and lastly Playerhandler cs:1046, it exists within an OnLevelWasLoaded method and simply calls the method that starts the co-routines off.
if(MainScene == -1)
{
DaniellaF = GameObject.Find("DaniellaF");
DoogerF = GameObject.Find("DoogerF");
OrphanF = GameObject.Find("OrphanMaster");
CSONE(); // line 1046
Debug.Log ("first time here, playing opening scene");
}
I’m terribly sorry this is so long, if i had any idea where the error was i’d post just that but i really have no clue. I’ve never had a null ref error like this one. I’ve tried looking to see if the rigidbody wasn’t being properly assigned but it shows up in the inspector fine after the script uses a .getComponent call and the co-routine plays the first three times fine. Thing is i get the error right at the START of the scene, it gets called successfully after the error shows up two or three times but then not for the last step, which isn’t called any differently.
I have identical scripts running with no issue, im so confused :s this is literally game-breaking, if you need anymore information or need to screenshare, i’m up for trying anything at this point. Reverted to backups, error still persists. take pity on a poor soul. I get the same error on the player script a bit later on but im hoping if i can solve this one i’ll be able to solve the other.