0
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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.

\$\endgroup\$
  • \$\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 at 2:00
  • \$\begingroup\$ You're not the first. ;) \$\endgroup\$ – DMGregory Jan 23 at 2:02

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.