I have a public array of length 9 containing prefabs I'm using as position references for a moving object. The start function finds the transform.position of the appropriate transform in the array and makes it the target for a Vector3.MoveTowards() function that is called every update. I have an exception for the start function, but the object moves fine. If I print the length of the array on every update, It shows zero every other frame, and 9 otherwise. There's nothing in the script changing the array, how is this happening?

I'm using the Linux build of Unity.


1 Answer 1


It sounds like you have a second copy of your script in your scene somewhere, with the array unpopulated.

Each frame, the correctly-configured instance prints a length of 9, and the incorrectly-configured instance prints a length of 0, making it look like you have alternation from frame to frame, when it's really coming from two different instances in the same frame.

Try Debug.Logging the name of your object and its path in the hierarchy in Start() (before the exception) - I bet you'll get two lines printing: one for the correctly-configured object, and one for the accidental dupe, helping you track it down and delete it.

  • \$\begingroup\$ Oh my god, you were right! Just searched for references to the script and saw it. How did you know? \$\endgroup\$
    – Hussar
    Jan 23, 2019 at 2:00
  • \$\begingroup\$ You're not the first. ;) \$\endgroup\$
    – DMGregory
    Jan 23, 2019 at 2:02

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .