Question by
viktoria93 · Jul 13, 2017 at 12:30 PM ·
scripting problemnullreferenceexceptionscript error
NullExecption in a communication between two scripts
This error drives me nuts. I create a method, which returns a string with all visible objects.
public static Renderer[] renderers;
public static List<string> objNr = new List<string>();
//...
public static string myMethod (Renderer[] renderers) {
foreach (var renderer in renderers) {
if (IsVisible (renderer)) {
objNr.Add (renderer.name);
}
}
string itemsInOneLine = string.Join (", ", objNr.ToArray());
return itemsInOneLine;
objNr.Clear ();
}
Another script takes captures from a model and should name them with all those above detected objects:
//taking screenshots just every few seconds
IEnumerator takingShots() {
while (Time.frameCount <= 434) {
string name = string.Format ("{0}/{1:D04}"
+ Camera.main.transform.position + "_"
+ CheckAllVisibleObjects.myMethod (CheckAllVisibleObjects.renderers) + ".png", folder,
Time.frameCount);
Application.CaptureScreenshot (name);
yield return new WaitForSeconds (0.1f);
}
}
So far it should work, but if I try to run both scripts, I get the error
NullReferenceException: Object reference not set to an instance of an object
CheckAllVisibleObjects.myMethod (UnityEngine.Renderer[] renderers) (at Assets/Scripts/CheckAllVisibleObjects.cs:25)
CaptureAlongPath+<takingShots>c__Iterator0.MoveNext () (at Assets/Scripts/CaptureAlongPath.cs:28)
UnityEngine.SetupCoroutine.InvokeMoveNext (IEnumerator enumerator, IntPtr returnValueAddress) (at C:/buildslave/unity/build/Runtime/Export/Coroutines.cs:17)
UnityEngine.MonoBehaviour:StartCoroutine(IEnumerator)
CaptureAlongPath:Start() (at Assets/Scripts/CaptureAlongPath.cs:17)
I know this error comes from the takingShots()-Method, but I can't fix it.
Thank you for any help!
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612130152im_/https://answers.unity.com/themes/thub/images/avi.jpg)